Ejemplo n.º 1
0
        protected override void OnOpen()
        {
            base.OnOpen();
            mConnectedTime = DateTime.Now;

            DebugUtility.LogTrace(LoggerTags.Online, "¡¾Server¡¿  Status : {0}, OnOpen, Time : {1}", this.ConnectionState, mConnectedTime.ToLongTimeString());
        }
Ejemplo n.º 2
0
 public void Disconnect()
 {
     DebugUtility.LogTrace(LoggerTags.Online, "Disconnect {0}", host);
     state = ENetState.Disconnected;
     BridgeCenter.Caller.DisconnnectAsync(host, "");
     BridgeCenter.Caller.onRecvMessageEvent -= OnRecvMessage;
     Misc.SafeInvoke(onDisconnected, ENetCode.WSNormal);
 }
Ejemplo n.º 3
0
        public void CreateServer()
        {
            DebugUtility.LogTrace(LoggerTags.Online, "¡¾Common¡¿ CreateServer : {0}", port);
            var server = new WebSocketServer(port);

            server.AddService <WebSocketServerHandleDemo>(serverService);
            server.StartListening();

            mServer = server;
        }
Ejemplo n.º 4
0
        protected override void OnClose(CloseEventArgs e)
        {
            base.OnClose(e);
            mDisconnectedTime = DateTime.Now;

            DebugUtility.LogTrace(
                LoggerTags.Online,
                "¡¾Server¡¿  Status : {0}, OnClose : [Code({1}), Reason({2})], Time : {3}, Alive : {4}(seconds)",
                this.ConnectionState, e.Code, e.Reason, mDisconnectedTime.ToLongTimeString(), (mDisconnectedTime - mConnectedTime).TotalSeconds);
        }
Ejemplo n.º 5
0
        public void Connect(string url)
        {
            this.host = url;
            DebugUtility.LogTrace(LoggerTags.Online, "Connect {0}", host);
            var center = BridgeCenter.Caller;

            center.onRecvMessageEvent -= OnRecvMessage;
            center.onRecvMessageEvent += OnRecvMessage;
            state = ENetState.Connected;
            BridgeCenter.Caller.ConnnectAsync(host, "");
        }
Ejemplo n.º 6
0
        private void CreateClient()
        {
            DebugUtility.LogTrace(LoggerTags.Online, "¡¾Common¡¿ CreateClient...");
            // Create WebSocket instance
            var ws = new WebSocketClient();

            // Add OnOpen event listener
            ws.onConnected += () =>
            {
                mConnectedTime = DateTime.Now;

                DebugUtility.LogTrace(LoggerTags.Online, "¡¾Client¡¿ connected : {0}", mConnectedTime.ToLongTimeString());
                DebugUtility.LogTrace(LoggerTags.Online, "¡¾Client¡¿ State : {0}", ws.state);

                ws.SendMessage(Encoding.UTF8.GetBytes("WebSocketDemo from Unity Client."));
            };

            // Add OnMessage event listener
            ws.onRecv += (byte[] msg) =>
            {
                DebugUtility.LogTrace(LoggerTags.Online, "¡¾Client¡¿ received message: " + Encoding.UTF8.GetString(msg));

                ws.Disconnect();
            };

            // Add OnError event listener
            ws.onError += (string errMsg) =>
            {
                DebugUtility.LogTrace(LoggerTags.Online, "¡¾Client¡¿ error: " + errMsg);
            };

            // Add OnClose event listener
            ws.onDisconnected += (code) =>
            {
                mDisconnectedTime = DateTime.Now;

                DebugUtility.LogTrace(LoggerTags.Online, "¡¾Client¡¿ closed with code: {0}, DisconnectedTime : {1}, Alive : {2}(seconds)", code, mDisconnectedTime.ToLongTimeString(), (mDisconnectedTime - mConnectedTime).TotalSeconds);
            };

            // Connect to the server
            if (createServer)
            {
                string url = string.Concat(serverHost, ":", port.ToString(), serverService);
                DebugUtility.Log(LoggerTags.Online, "¡¾Common¡¿ CreateClient {0}", url);
                ws.Connect(url);
            }
            else
            {
                DebugUtility.Log(LoggerTags.Online, "¡¾Common¡¿ CreateClient {0}", host);
                ws.Connect(host);
            }
            mClient = ws;
        }
Ejemplo n.º 7
0
        public void Connect(string host)
        {
            if (mConn != null && !mConn.state.IsDisconnected())
            {
                DebugUtility.LogError(LoggerTags.Online, "Please disconnect this conn.");
                return;
            }
            DebugUtility.LogTrace(LoggerTags.Online, "Connnect to {0}", host);

            const string kSimProtocolStr = "sim://";

            // sim : simulator
            if (host.StartsWith(kSimProtocolStr))
            {
                string name       = host.Substring(kSimProtocolStr.Length);
                var    simulators = UnityEngine.Object.FindObjectsOfType <ScratchWebSocketClientSimulator>();
                foreach (var simulator in simulators)
                {
                    if (simulator.name.StartsWith(name, StringComparison.OrdinalIgnoreCase))
                    {
                        mConn = simulator;
                    }
                    if (mConn != null)
                    {
                        break;
                    }
                }

                if (mConn != null)
                {
                    mConn.onConnected    = OnConnected;
                    mConn.onRecv         = OnMessage;
                    mConn.onError        = OnError;
                    mConn.onDisconnected = OnDisconnected;
                    mConn.Connect(host);
                }
                else
                {
                    mConn = NetworkFactory.CreateWebSocketSimulator <ScratchWebSocketClientSimulator>(host, true, OnConnected, OnDisconnected, OnMessage, OnError);
                }
                return;
            }
            if (host.StartsWith(WebBridgeClient.ProtocolHeader))
            {
                mConn = NetworkFactory.CreateWebClient <WebBridgeClient>(host, true, OnConnected, OnDisconnected, OnMessage, OnError);
                return;
            }
            mConn = NetworkFactory.CreateWebClient(host, true, OnConnected, OnDisconnected, OnMessage, OnError);
        }
Ejemplo n.º 8
0
        private void OnEngineInitialized(IEngine engine)
        {
            DebugUtility.LogTrace(LoggerTags.Engine, "OnEngineInitialized");
            var currentScene = SceneManager.GetActiveScene();

            // temp code
            if (!string.IsNullOrEmpty(modelAssetName))
            {
                LoadModel(FileSystem.AnyPathToResourcesPath(modelAssetName, true), true);
                return;
            }

            if (sceneName != currentScene.name)
            {
                SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);
            }
            else
            {
                SceneManager.LoadSceneAsync(1, LoadSceneMode.Single);
            }
        }
Ejemplo n.º 9
0
        protected override void OnError(ErrorEventArgs e)
        {
            base.OnError(e);

            DebugUtility.LogTrace(LoggerTags.Online, "¡¾Server¡¿  Status : {0}, OnClose : [Message({1}), Exception({2})]", this.ConnectionState, e.Message, e.Exception);
        }
Ejemplo n.º 10
0
        protected override void OnMessage(MessageEventArgs e)
        {
            base.OnMessage(e);

            DebugUtility.LogTrace(LoggerTags.Online, "¡¾Server¡¿  Status : {0}, OnMessage : {1}", this.ConnectionState, Encoding.UTF8.GetString(e.RawData));
        }
Ejemplo n.º 11
0
        protected void Start()
        {
            DebugUtility.LogTrace("Startup", "Startup");

            ULokiEngine.onEngineInitialized = OnEngineInitialized;
        }