예제 #1
0
void FCServer::cbJsonMessage(libwebsocket *wsi, rapidjson::Document &message, void *context)
{
    // Received a JSON message from a WebSockets client.
    // Replies are formed by modifying the original message.

    FCServer *self = (FCServer*) context;

    const Value &vtype = message["type"];
    if (!vtype.IsString()) {
        lwsl_notice("NOTICE: Received JSON is missing mandatory \"type\" string\n");
        return;
    }
    const char *type = vtype.GetString();

    // Hold the event lock while dispatching
    self->mEventMutex.lock();

    if (!strcmp(type, "list_connected_devices")) {
        self->jsonListConnectedDevices(message);
    } else if (!strcmp(type, "server_info")) {
        self->jsonServerInfo(message);
    } else if (message.HasMember("device")) {
        self->jsonDeviceMessage(message);
    } else {
        message.AddMember("error", "Unknown message type", message.GetAllocator());
    }

    self->mEventMutex.unlock();

    // Remove heavyweight members we should never reply with
    message.RemoveMember("pixels");

    // All messages get a reply, and we leave any extra parameters on the message
    // so that clients can keep track of asynchronous completions.
    self->mNetServer.jsonReply(wsi, message);
}