Пример #1
0
        // Event triggers when connection is comming
        // It creates socket, and for now statically creates it as WebSockets client
        // As well it creates User instance in World for GameLoop
        private void OnAccept(IAsyncResult result)
        {
            try {
                SocketClient client = null;
                lock (this.locker) {
                    if (this.socket != null && this.socket.IsBound)
                    {
                        //Log.Add("server accepting");
                        client = new ClientWS(this.socket.EndAccept(result));
                    }
                }
                if (client != null)
                {
                    if (client.Start())
                    {
                        this.publisher.Subscribe(client);

                        client.User        = this.instance.Game.World.AddUser("guest" + client.ID);
                        client.Stopped    += (obj) => obj.User = null;
                        client.User.Client = client;

                        /*client.Stopped += delegate(SocketClient obj) {
                         * obj.User.World.RemoveUser(client.User);
                         * };*/
                        /*DataPacket packet = this.StatsPacket;
                         * if (packet != null) {
                         * client.Send(packet);
                         * }*/
                    }
                    else
                    {
                        client.Stop();
                    }
                }
            } catch (SocketException exception) {
                Log.Add("server OnAccept exception: " + exception.ToString());
            } finally {
                lock (this.locker) {
                    if (this.socket != null && this.socket.IsBound)
                    {
                        socket.BeginAccept(null, 0, OnAccept, null);
                    }
                }
            }
        }
Пример #2
0
    void Start()
    {
        if (AutoDetectPlatform)
        {
            if (Application.platform == RuntimePlatform.WebGLPlayer)
            {
                Platform = platform.WebGL;
            }
            if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
            {
                Platform = platform.Standalone;
            }
        }

        if (Platform == platform.Standalone)
        {
            client = new ClientWS();

            client.ChooseOpenMethod(onOpen);
            client.ChooseCloseMethod(onClose);
            client.ChooseMessageMethod(onMessage);
            client.ChooseErrorMethod(onError);
        }
    }