コード例 #1
0
    /**
     * Starts listening for websocket connections of controllers, as soon as
     * this game object is awakened.
     */
    void Awake()
    {
        _baseServer = new BaseServer();

        // The `ControllerConnection` class will handle remote controller connections on a different
        // thread.
        _baseServer.AddWebSocketService <ControllerConnection>(
            "/controllers",      // we expect connections on the /controllers path
            connection =>
        {
            // we use this initialization function to pass the message queue
            // to the connection worker and remember the id of the connection
            connection.Initialize(_nextNumericConnectionId++, _incomingConnectionUpdates);
            // ^ NOTICE: As far as I understand it, reference assignments as performed by this method  should be
            //           thread-safe. However, if strange things happen, we should look here first.
            Log("A new connection has been established.");
        });

        // Furthermore, we provide a websocket service offering diagnostic data about the game.
        _baseServer.AddWebSocketService <DiagnosticsConnection>("/diagnostics");

        // Now we need to get a handle to the session manager which
        // keeps track of all connections, so that we can use it later
        // to send data
        WebSocketServiceHost serviceHost;

        if (_baseServer.WebSocketServices.TryGetServiceHost("/controllers", out serviceHost))
        {
            _wssm = serviceHost.Sessions;
        }

        else
        {
            Log("Could not get a hold of the websocket service host. This means we can not send any messages.");
        }

        _baseServer.Start();
    }