当前位置: 首页>>代码示例>>C++>>正文


C++ JSONValue::FromString方法代码示例

本文整理汇总了C++中JSONValue::FromString方法的典型用法代码示例。如果您正苦于以下问题:C++ JSONValue::FromString方法的具体用法?C++ JSONValue::FromString怎么用?C++ JSONValue::FromString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在JSONValue的用法示例。


在下文中一共展示了JSONValue::FromString方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: Update


//.........这里部分代码省略.........
    for (uint i=0; i < processEvents.Size(); ++i)
    {
        SocketEvent *event = processEvents[i];
        if (!event)
            continue;

        // User connected
        if (event->type == SocketEvent::Connected)
        {
            if (!UserConnection(event->connection))
            {
                WebSocket::UserConnectionPtr userConnection(new WebSocket::UserConnection(context_, event->connection));
                connections_.Push(userConnection);

                // The connection does not yet have an ID assigned. Tundra server will assign on login
                LogDebug(LC + String("New WebSocket connection."));
            }
        }
        // User disconnected
        else if (event->type == SocketEvent::Disconnected)
        {
            for(UserConnectionList::Iterator iter = connections_.Begin(); iter != connections_.End(); ++iter)
            {
                if ((*iter) && (*iter)->WebSocketConnection() == event->connection)
                {
                    tundraServer->RemoveExternalUser(Urho3D::StaticCast<::UserConnection>(*iter));
                    if (!(*iter)->userID)
                        LogDebug(LC + "Removing non-logged in WebSocket connection.");
                    else
                        LogInfo(LC + "Removing WebSocket connection with ID " + String((*iter)->userID));
                    connections_.Erase(iter);
                    break;
                }
            }
        }
        // Data message
        else if (event->type == SocketEvent::Data && event->data.get())
        {
            WebSocket::UserConnectionPtr userConnection = UserConnection(event->connection);
            if (userConnection)
            {
                kNet::DataDeserializer dd(event->data->GetData(), event->data->BytesFilled());
                u16 messageId = dd.Read<u16>();

                // LoginMessage
                if (messageId == cLoginMessage)
                {
                    bool ok = false;
                    String loginDataString = ReadUtf8String(dd);
                    // Read optional protocol version
                    if (dd.BytesLeft())
                        userConnection->protocolVersion = (NetworkProtocolVersion)dd.ReadVLE<kNet::VLE8_16_32>();

                    JSONValue json;
                    ok = json.FromString(loginDataString);
                    if (ok)
                    {
                        JSONObject jsonObj = json.GetObject();
                        for (JSONObject::ConstIterator i = jsonObj.Begin(); i != jsonObj.End(); ++i)
                            userConnection->properties[i->first_] = i->second_.ToVariant();
                        userConnection->properties["authenticated"] = true;
                        bool success = tundraServer->AddExternalUser(Urho3D::StaticCast<::UserConnection>(userConnection));
                        if (!success)
                        {
                            LogInfo(LC + "Connection ID " + String(userConnection->userID) + " login refused");
                            toDisconnect.Push(userConnection);
                        }
                        else
                            LogInfo(LC + "Connection ID " + String(userConnection->userID) + " login successful");
                    }
                }
                else
                {
                    // Only signal messages from authenticated users
                    if (userConnection->properties["authenticated"].GetBool() == true)
                    {
                        // Signal network message. As per kNet tradition the message ID is given separately in addition with the rest of the data
                        NetworkMessageReceived.Emit(userConnection.Get(), messageId, event->data->GetData() + sizeof(u16), event->data->BytesFilled() - sizeof(u16));
                        // Signal network message on the Tundra server so that it can be globally picked up
                        tundraServer->EmitNetworkMessageReceived(userConnection.Get(), 0, messageId, event->data->GetData() + sizeof(u16), event->data->BytesFilled() - sizeof(u16));
                    }
                }
            }
            else
                LogError(LC + "Received message from unauthorized connection, ignoring.");

            event->data.reset();
        }
        else
            event->data.reset();
        
        SAFE_DELETE(event);
    }

    for (uint i = 0; i < toDisconnect.Size(); ++i)
    {
        if (toDisconnect[i])
            toDisconnect[i]->Disconnect();
    }
}
开发者ID:realXtend,项目名称:tundra-urho3d,代码行数:101,代码来源:WebSocketServer.cpp


注:本文中的JSONValue::FromString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。