Пример #1
0
        public void createConnect()
        {
            //socketIO = new Client("http://168.63.151.29:3000");

            socketIO = new Client("http://test.twtstudio.com:3000/");
            socketIO.Message += socketIO_Message;
            socketIO.SocketConnectionClosed += socketIO_SocketConnectionClosed;
            socketIO.Error += socketIO_Error;

            socketIO.On("connect", (message) =>
            {
                Debug.WriteLine("on connect called!!!");
                JObject jo = new JObject();
                jo["username"] = "******";
                socketIO.Emit("join", jo);
            });


            socketIO.On("chat", async (message) =>
            {
                Debug.WriteLine("start listening");
                Debug.WriteLine(message.Json.ToJsonString());
                await SampleDispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    JObject o = (JObject)JsonConvert.DeserializeObject(message.Json.ToJsonString());
                    JArray jb = (JArray)JsonConvert.DeserializeObject(o["args"].ToString());
                    JObject ob = (JObject)jb[0];

                    if (ob["roomNum"].ToString().Equals(Self.roomNum.ToString()))
                        ChangedEventHandler(ob["username"]+":"+ob["chat"]);
                });
            });

            socketIO.ConnectAsync();
        }
Пример #2
0
        private void connectBt_Click(object sender, RoutedEventArgs e)
        {
            socketIO = new Client(serverUrl.Text);
            socketIO.Message += socketIO_Message;
            socketIO.SocketConnectionClosed += socketIO_SocketConnectionClosed;
            socketIO.Error += socketIO_Error;

            socketIO.On("connect", (message) =>
            {
                Debug.WriteLine("on connect called!!!");
                JObject jo = new JObject();
                jo["publisher"] = "username";
                jo["password"] = "******";
                socketIO.Emit("hConnect", jo);
            });
            socketIO.ConnectAsync();
        }
        public void Connect(HTransportOptions options)
        {
            this.connStatus = ConnectionStatus.CONNECTING;
            this.options = options;

            //TODO init the connection timeout value!!
            connTimeout = new TimeSpan(0, 0, 0, 0, options.Timeout);

            string endpointHost = options.EndpointHost;
            int endpointPort = options.EndpointPort;
            string endpointPath = options.EndpointPath;

            string endpointAdress = ToEndpointAdress(endpointHost, endpointPort, endpointPath);

            connTimeoutTimer = ThreadPoolTimer.CreateTimer(timeout_Elapsed, connTimeout);

            socketIO = new Client(endpointAdress);

            socketIO.Message += socketIO_Message;
            socketIO.SocketConnectionClosed += socketIO_SocketConnectionClosed;
            socketIO.Error += socketIO_Error;
            socketIO.On("connect", (message) =>
                {
                    if (this.options.AuthCb != null)
                        this.options.AuthCb(options.Login, Login);
                    else
                        Login(options.Login, options.Password);
                });
            socketIO.ConnectAsync();
        }
Пример #4
-3
        //哭了,貌似socketIO中on事件里会发生这个闹心的问题啊
        //他的相当于另起一个线程,然后里边的东西变成了局部变量,不能改变这个class的值啊  疯了!!!
        //那怎么玩啊、、弄个static试试
        //nice啊 静态也不行。哭了哭了 这次真哭了!
        private void connectBt_Click(object sender, RoutedEventArgs e)
        {
            //socketIO = new Client(serverUrl.Text);
            //socketIO = new Client("http://168.63.151.29:3000");
            this.connectBt.IsEnabled = false;
            socketIO = new Client("http://test.twtstudio.com:3000");
            socketIO.Message += socketIO_Message;
            socketIO.SocketConnectionClosed += socketIO_SocketConnectionClosed;
            socketIO.Error += socketIO_Error;

            socketIO.On("connect", (message) =>
            {

                Debug.WriteLine("on connect called!!!");
                JObject jo = new JObject();
                jo["username"] = "******";
                jo["password"] = "******";
                socketIO.Emit("hConnect", jo);

            });

            socketIO.On("a", async (message) =>
            {
                Debug.WriteLine("start listening");
                Debug.WriteLine(message.Json.ToJsonString());
                await SampleDispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    /*//定义一个JSON字符串
                    string jsonText = "[{'a':'aaa','b':'bbb','c':'ccc'},{'a':'aaa2','b':'bbb2','c':'ccc2'}]";
                    //反序列化JSON字符串*/
                    //JArray ja = (JArray)JsonConvert.DeserializeObject(message.Json.ToJsonString());
                   // JArray ja = (JArray)JsonConvert.DeserializeObject(jsonText);
                    //将反序列化的JSON字符串转换成对象
                    JObject o = (JObject)JsonConvert.DeserializeObject(message.Json.ToJsonString());
                    //读取对象中的各项值
                    JArray jb = (JArray)JsonConvert.DeserializeObject(o["args"].ToString());

                    JObject ob = (JObject)jb[0];

                    JObject oc = (JObject)ob["mes"];

                    //Console.WriteLine(ja[1]["a"]);

                   // ChangedEventHandler(message.Json.ToJsonString());

                    if(oc["a"] != null )
                        ChangedEventHandler(oc["a"].ToString());
                    //{"name":"a","args":[{"mes":{"username":"******","password":"******"}}]}
                });
            });

            socketIO.ConnectAsync();
        }