Dispose() public method

public Dispose ( ) : void
return void
        static void connectSocket()
        {
            try
            {
                socket = new Client(ConfigParam.Rows[0]["Host"].ToString());
                socket.Error += socket_Error;
                socket.Opened += socket_Opened;
                socket.ConnectionRetryAttempt += socket_ConnectionRetryAttempt;
                socket.SocketConnectionClosed += socket_SocketConnectionClosed;
                myForm.Socket = socket;
                socket.On("welcome", (message) =>
                {
                    //MessageBox.Show(data.RawMessage);
                    String msg = message.Json.Args[0].ToString();
                    dynamic json = JObject.Parse(msg);
                    Console.Write(json.message);
                    myForm.ConsoleWriteLine(json.message.ToString());

                    dynamic respon = JObject.Parse("{AppID: '" + ConfigParam.Rows[0]["AppID"].ToString() + "'}");

                    socket.Emit("regAppID", respon);
                    //MessageBox.Show(msg, "Received Data");
                });

                socket.On("time", (data) =>
                {
                    //MessageBox.Show(data.RawMessage);
                    String msg = data.Json.Args[0].ToString();
                    dynamic json = JObject.Parse(msg);

                    Console.Write(json.time);
                    myForm.ConsoleWriteLine(json.time.ToString());
                    socket.Emit("i am client", "{data: '" + msg + "'}");
                    //MessageBox.Show(msg, "Received Data");
                });

                socket.On("dMessage", (data) =>
                {
                    //MessageBox.Show(data.RawMessage);
                    String msg = data.Json.Args[0].ToString();
                    dynamic json = JObject.Parse(msg);

                    myForm.ConsoleWriteLine(json.message.ToString());
                    //socket.Emit("i am client", "{data: '" + msg + "'}");
                    //MessageBox.Show(msg, "Received Data");
                });

                socket.On("request_transaction", (data) =>
                {
                    String msg = data.Json.Args[0].ToString();
                    dynamic json = JObject.Parse(msg);
                    myForm.ConsoleWriteLine("Get transaction on date : " + json.date.ToString());
                    ExecuteCommand("", "request_transaction", "");
                });

                socket.On("fdCommand", (data) =>
                {
                    String msg = data.Json.Args[0].ToString();
                    dynamic json = JObject.Parse(msg);

                    myForm.ConsoleWriteLine("command : " + json.command.ToString() + " ; parameter : " + json.parameter.ToString());

                    //dynamic parameter = JObject.Parse(json.parameter.ToString());
                    //myForm.ConsoleWriteLine("device : " + parameter.device.ToString());

                    //Store command to command datatable
                    AddCommandLine(json.commandID.ToString(), json.command.ToString(), json.parameter.ToString(), "not_exec");
                    ExecuteCommand(json.commandID.ToString(), json.command.ToString(), json.parameter.ToString());


                    /*dynamic responJson = new JObject();
                    responJson.commandID = "";
                    responJson.appID = myForm.getAppID();
                    responJson.respons = json.command.ToString();

                    

                    socket.Emit("fdCommandRespons", responJson);*/
                });

                socket.Connect();

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString(), "Something Went Wrong!!");
                Application.Exit();
            }

            if (socket.ReadyState == WebSocket4Net.WebSocketState.None)
            {
                socket.Dispose();
                socket.Error -= socket_Error;
                socket.Opened -= socket_Opened;
                socket.ConnectionRetryAttempt -= socket_ConnectionRetryAttempt;
                socket.SocketConnectionClosed -= socket_SocketConnectionClosed;
                myForm.Socket.Dispose();
                myForm.Socket = null;
                socket = null;
                
                connectSocket();
            } 
        }
Beispiel #2
0
        private static void Target(object parameters)
        {
            var options = (ThreadParams)parameters;
            Random rand = new Random(options.Seed);

            if (options.State == ThreadState.CreateGame)
            {
                Thread.Sleep(rand.Next(0, 500));
            }

            if (options.State == ThreadState.JoinGame)
            {
                Thread.Sleep(rand.Next(1500, 2500));
            }

            Console.WriteLine(string.Format("Begin {0}", options.State));
            string ip=null;

            WebClient client = new WebClient();
            ip = client.DownloadString("http://50.116.22.241:8844");

            var socket = new Client(ip); // url to nodejs
            socket.Opened += SocketOpened;
            socket.Message += SocketMessage;
            socket.SocketConnectionClosed += SocketConnectionClosed;
            socket.Error += SocketError;
            string gameServer=null;
            string roomID = null;
            // register for 'connect' event with io server

            Dictionary<string, Action<dynamic>> dct = new Dictionary<string, Action<dynamic>>();

            socket.On("Client.Message", (fn) =>
                                            {
                                                var cn = fn.Json.Args[0].channel.Value;
                                                var cnt = fn.Json.Args[0].content;

                                                dct[cn](cnt);

                                            });

            dct.Add("Area.Game.AskQuestion", (fn) =>
            {
                Thread.Sleep(rand.Next(300, 800));
                Console.WriteLine("asked: " + "  " + options.UserName);
                if (socket == null) return;
                try
                {
                    emit(socket, "Area.Game.AnswerQuestion", new { answer = 1, roomID }, gameServer);
                }catch(Exception)
                {
                    Console.WriteLine("failed for some reason");
                }
            });

            dct.Add("Area.Game.UpdateState", (fn) =>
            {

            });

            dct.Add("Area.Game.RoomInfo", (fn) =>
                                                {
                                                    roomID = fn.roomID.ToString();
                                                    gameServer = fn.gameServer;
                                                });

            bool gameover = false;
            dct.Add("Area.Game.GameOver", (fn) =>
                                              {
                                                  socket.Close();
                                                  socket.Dispose();
                                                  gameover = true;

                                              });
            dct.Add("Area.Game.RoomInfos", (data) =>
                                                 {

                                                     foreach (var room in data.Children())
                                                     {
                                                         var plys = 0;
                                                         foreach (var child in room["players"].Children())
                                                         {
                                                             plys++;
                                                         }
                                                         if (room["started"].Value)
                                                         {
                                                             continue;
                                                         }
                                                         gameServer = room["gameServer"].Value;
                                                         switch (options.State)
                                                         {
                                                             case ThreadState.JoinGame:
                                                                 roomID = room["roomID"].Value;
                                                                 emit(socket, "Area.Game.Join", new { user = new { userName = options.UserName }, roomID = room["roomID"].Value }, gameServer);

                                                                 if (plys + 1 >= options.MaxUsers)
                                                                 {
                                                                     Thread.Sleep(rand.Next(750, 1250));
                                                                     emit(socket, "Area.Game.Start", new { roomID = room["roomID"].Value }, gameServer);
                                                                     return;
                                                                 }
                                                                 return;
                                                                 break;
                                                             default:
                                                                 throw new ArgumentOutOfRangeException();
                                                         }
                                                     }

                                                     Thread.Sleep(rand.Next(600, 900));
                                                     emit(socket, "Area.Game.GetGames", true, gameServer);
                                                 });
            dct.Add("Area.Game.Started", (fn) =>
            {

            });

            // make the socket.io connection
            socket.Connect();

            socket.Emit("Gateway.Login", new {userName = options.UserName + " " + Guid.NewGuid().ToString()});

            switch (options.State)
            {
                case ThreadState.JoinGame:
                    emit(socket, "Area.Game.GetGames", true, gameServer);
                    break;
                case ThreadState.CreateGame:
                    Console.WriteLine( "Created");

                    emit(socket, "Area.Game.Create", new { gameName = "Sevens", name = "game " + Guid.NewGuid().ToString()  , user = new { name = options.UserName } }, gameServer);

                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
            while (!gameover)
            {
                Thread.Sleep(1000);
            }
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Starting TestSocketIOClient Example...");
            socket = new Client("http://localhost:8008/"); // url to nodejs server
            socket.Error += SocketError;

            // register for 'connect' event with io server
            socket.On("connect", (fn) =>
            {
                Console.WriteLine("\r\nConnected event...\r\n");
            });

            socket.On("news", (data) =>
            {
                Console.WriteLine("  raw message:      {0}", data.RawMessage);
                Console.WriteLine("  string message:   {0}", data.MessageText);
            });

            socket.On("acceptPosition", (data) =>
                {
                    var x = ((int) data.Json.Args[0].x.Value);
                    var y = ((int) data.Json.Args[0].y.Value);
                    var point = new System.Drawing.Point(x,y);
                    //Console.WriteLine("external: " + point);
                    SetCursorPos(point.X, point.Y);
                });

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("\r\nPress 'c' to execute checkin event \r\n\t(view socket.io server console window to verify).\r\nPress 'x' to exit.");
            Console.ResetColor();

            // make the socket.io connection
            socket.Connect();

            _hookID = SetHook(_proc);
            Application.Run();
            UnhookWindowsHookEx(_hookID);

            socket.Dispose();
            //Execute();
        }