Close() public method

Close SocketIO4Net.Client and clear all event registrations
public Close ( ) : void
return void
Ejemplo n.º 1
0
    public void CloseConnection()
    {
        Debug.Log("Closing");

        _socket.Close();
        ClearEvents();
    }
Ejemplo n.º 2
0
 public void disconnect()
 {
     if (socket != null)
     {
         socket.Close();
     }
 }
Ejemplo n.º 3
0
    void OnGUI()
    {
        if (GUI.Button(new Rect(20, 120, 150, 30), "Close Connection"))
        {
            Debug.Log("Closing");

            socket.Close();
        }
    }
Ejemplo n.º 4
0
 void OnGUI()
 {
     if (GUI.Button(new Rect(20, 70, 150, 30), "SEND"))
     {
         Debug.Log("Sending");
         Dictionary <string, string> args = new Dictionary <string, string>();
         args.Add("msg", "hello!");
         socket.Emit("SEND", args);
     }
     if (GUI.Button(new Rect(20, 120, 150, 30), "Close Connection"))
     {
         Debug.Log("Closing");
         socket.Close();
     }
 }
Ejemplo n.º 5
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);
            }
        }
Ejemplo n.º 6
0
 void OnDisable()
 {
     client.Close();
 }
Ejemplo n.º 7
0
    private void CreateSocket(string name, string password)
    {
        _socket = new SocketIOClient.Client("http://" + _baseAddress + "/ ");
        _socket.On("connect", (fn) =>
        {
            Debug.Log("connect - socket");

            Dictionary <string, string> args = new Dictionary <string, string>();
            args.Add("pseudo", name);
            args.Add("password", password);
            _socket.Emit("authentication", args);
            _socket.On("unauthorized", (data) =>
            {
                Debug.Log("unauthorized");
                if (Unauthorized != null)
                {
                    Unauthorized(JSON.Parse(data.Encoded));
                }
                _socket.Close();
            });
            _socket.On("authenticated", (data) =>
            {
                Debug.Log("authenticated");
                if (Authenticated != null)
                {
                    Authenticated(JSONNode.Parse(data.Encoded));
                }

                _socket.On("lobbylist", (result) =>
                {
                    if (LobbyList != null)
                    {
                        JSONNode resultjson    = JSONNode.Parse(result.Encoded);
                        JSONArray arguments    = resultjson["args"].AsArray;
                        JSONArray actualresult = arguments[0].AsArray;
                        LobbyList(actualresult);
                    }
                });
                _socket.On("lobbycreated", (result) =>
                {
                    if (LobbyCreated != null)
                    {
                        LobbyCreated(JSONNode.Parse(result.Encoded));
                    }
                });
                _socket.On("lobbyfull", (result) =>
                {
                    if (LobbyFull != null)
                    {
                        LobbyFull(JSONNode.Parse(result.Encoded));
                    }
                });
                _socket.On("lobbyjoined", (result) =>
                {
                    if (LobbyJoined != null)
                    {
                        JSONNode resultjson = JSONNode.Parse(result.Encoded)["args"][0];
                        LobbyJoined(resultjson);
                    }
                });
                _socket.On("lobbyleft", (result) =>
                {
                    if (LobbyLeft != null)
                    {
                        LobbyLeft(JSONNode.Parse(result.Encoded));
                    }
                });
                _socket.On("readyset", (result) =>
                {
                    if (ReadySet != null)
                    {
                        JSONNode resultjson = JSONNode.Parse(result.Encoded)["args"][0];
                        ReadySet(resultjson);
                    }
                });
                _socket.On("goingame", (result) =>
                {
                    if (GoInGame != null)
                    {
                        GoInGame(JSONNode.Parse(result.Encoded)["args"][0]);
                    }
                });
                _socket.On("unreadyset", (result) =>
                {
                    if (UnReadySet != null)
                    {
                        UnReadySet(JSONNode.Parse(result.Encoded)["args"][0]);
                    }
                });
                _socket.On("cantunready", (result) =>
                {
                    if (CantUnready != null)
                    {
                        CantUnready(JSONNode.Parse(result.Encoded)["args"][0]);
                    }
                });
                _socket.On("countdown", (result) =>
                {
                    if (Countdown != null)
                    {
                        Countdown(JSONNode.Parse(result.Encoded)["args"][0]);
                    }
                });
                _socket.On("playerjoined", (result) =>
                {
                    if (PlayerJoined != null)
                    {
                        PlayerJoined(JSONNode.Parse(result.Encoded)["args"][0]);
                    }
                });
                _socket.On("playerleft", (result) =>
                {
                    if (PlayerLeft != null)
                    {
                        PlayerLeft(JSONNode.Parse(result.Encoded)["args"][0]);
                    }
                });
                _socket.On("opponentreadyup", (result) =>
                {
                    if (OpponentReadyUp != null)
                    {
                        OpponentReadyUp(JSONNode.Parse(result.Encoded)["args"][0]);
                    }
                });
                _socket.On("opponentunready", (result) =>
                {
                    if (OpponentUnready != null)
                    {
                        OpponentUnready(JSONNode.Parse(result.Encoded)["args"][0]);
                    }
                });
                _socket.On("update", (result) =>
                {
                    if (Update != null)
                    {
                        Update(JSONNode.Parse(result.Encoded)["args"][0]);
                    }
                });
                _socket.On("gameover", (result) =>
                {
                    if (Gameover != null)
                    {
                        Gameover(JSONNode.Parse(result.Encoded)["args"][0]);
                    }
                });
                _socket.On("result", (result) =>
                {
                    if (Result != null)
                    {
                        Result(JSONNode.Parse(result.Encoded)["args"][0]);
                    }
                });
            });
        });

        _socket.Error += (sender, e) => {
            Debug.Log("socket Error: " + e.Message.ToString());
            if (Error != null)
            {
                Error(e.Message.ToString());
            }
        };
    }
Ejemplo n.º 8
0
    void OnGUI()
    {
        lobbyname = GUI.TextField(new Rect(180, 30, 250, 30), lobbyname, 40);
        pseudo    = GUI.TextField(new Rect(700, 30, 200, 30), pseudo, 40);;
        pass      = GUI.TextField(new Rect(700, 70, 200, 30), pass, 40);;

        if (GUI.Button(new Rect(20, 70, 150, 30), "listlobby"))
        {
            Debug.Log("listlobby");

            Dictionary <string, string> args = new Dictionary <string, string>();
            socket.Emit("listlobby", args);
        }

        if (GUI.Button(new Rect(180, 70, 150, 30), "createlobby"))
        {
            Debug.Log("createlobby");

            Dictionary <string, string> args = new Dictionary <string, string>();
            if (string.IsNullOrEmpty(lobbyname))
            {
                args.Add("lobbyName", "Test");
            }
            else
            {
                args.Add("lobbyName", lobbyname);
            }
            socket.Emit("createlobby", args);
        }

        if (GUI.Button(new Rect(340, 70, 150, 30), "joinlobby"))
        {
            Debug.Log("joinlobby");

            Dictionary <string, string> args = new Dictionary <string, string>();
            if (string.IsNullOrEmpty(lobbyname))
            {
                args.Add("lobbyName", "Test");
            }
            else
            {
                args.Add("lobbyName", lobbyname);
            }
            socket.Emit("joinlobby", args);
        }

        if (GUI.Button(new Rect(20, 120, 150, 30), "Open Connection"))
        {
            Debug.Log("Opening");
            CreateSocket();
            socket.Connect();
        }

        if (GUI.Button(new Rect(180, 120, 150, 30), "Close Connection"))
        {
            Debug.Log("Closing");

            socket.Close();
        }

        if (GUI.Button(new Rect(340, 120, 150, 30), "leave lobby"))
        {
            Debug.Log("leaving");
            Dictionary <string, string> args = new Dictionary <string, string>();
            socket.Emit("leavelobby", args);
        }

        if (GUI.Button(new Rect(700, 120, 150, 30), "Create user"))
        {
            StartCoroutine(UserCreationRequest());
        }
    }
Ejemplo n.º 9
0
    private void CreateSocket()
    {
        socket = new SocketIOClient.Client("http://127.0.0.1:3000/");
        socket.On("connect", (fn) => {
            Debug.Log("connect - socket");

            Dictionary <string, string> args = new Dictionary <string, string>();
            args.Add("pseudo", "toto");
            args.Add("password", "tata");
            socket.Emit("authentication", args);
            socket.On("unauthorized", (data) => {
                Debug.Log("unauthorized");
                socket.Close();
            });
            socket.On("authenticated", (data) => {
                Debug.Log("authenticated");
                socket.On("result", (result) =>
                {
                    Debug.Log("command result = " + result);
                });
                socket.On("lobbylist", (result) =>
                {
                    Debug.Log("lobby list = " + result.Json.ToJsonString());
                });
                socket.On("lobbycreated", (result) =>
                {
                    Debug.Log("lobby created // " + result.Json.ToJsonString() + "//");
                });
                socket.On("lobbyjoined", (result) =>
                {
                    Debug.Log("lobby joined // " + result.Json.ToJsonString() + "//");
                });
                socket.On("lobbyleft", (result) =>
                {
                    Debug.Log("lobby left");
                });
                socket.On("readyset", (result) =>
                {
                    Debug.Log("readyset");
                });
                socket.On("goingame", (result) =>
                {
                    Debug.Log("goingame");
                });
                socket.On("unreadyset", (result) =>
                {
                    Debug.Log("unreadyset");
                });
                socket.On("cantunready", (result) =>
                {
                    Debug.Log("cantunready");
                });
                socket.On("countdown", (result) =>
                {
                    Debug.Log("countdown");
                });
                socket.On("playerjoined", (result) =>
                {
                    Debug.Log("playerjoined");
                });
                socket.On("playerleft", (result) =>
                {
                    Debug.Log("playerleft");
                });
                socket.On("opponentreadyup", (result) =>
                {
                    Debug.Log("opponentreadyup");
                });
                socket.On("opponentunready", (result) =>
                {
                    Debug.Log("opponentunready");
                });
                socket.On("update", (result) =>
                {
                    Debug.Log("update");
                });
                socket.On("gameover", (result) =>
                {
                    Debug.Log("gameover");
                });
                socket.On("result", (result) =>
                {
                    Debug.Log("result");
                });
            });
        });

        socket.Error += (sender, e) => {
            Debug.Log("socket Error: " + e.Message.ToString());
        };
    }
Ejemplo n.º 10
0
 private void OnApplicationQuit()
 {
     print("end of game");
     socket.Close();
 }
Ejemplo n.º 11
0
    public void OnGUI()
    {
        //label for position

        GUILayout.Label("position: " + positionData);


        GUILayout.Label("relx: " + relx);

        GUILayout.Label("relz: " + relz);

        GUILayout.Label("myusername:  "******"SEND messageer"))
        {
            //Debug.Log ("Sending");
            Dictionary <string, string> args = new Dictionary <string, string>();
            args.Add("msg", "hello!");
            socket.Emit("send message", args);
        }

        if (GUI.Button(new Rect(320, 170, 150, 30), "get all users on socket"))
        {
            //Debug.Log ("Sending");
            Dictionary <string, string> args = new Dictionary <string, string>();
            args.Add("msg", "hello!");
            socket.Emit("getallppl", args);
        }

        if (GUI.Button(new Rect(520, 170, 150, 30), "sendpos"))
        {
            //Debug.Log ("Sending");
            Dictionary <string, string> args = new Dictionary <string, string>();
            args.Add("0.22", "0.222");
            socket.Emit("sendpos", args);
        }


        PlayerUsername = GUI.TextArea(new Rect(20, 20, 200, 50), PlayerUsername, 100);

        if (GUI.Button(new Rect(120, 70, 150, 30), "SEND adduser name"))
        {
//			Dictionary<string, string> args = new Dictionary<string, string>();

            socket.Emit("adduser", PlayerUsername);


            PlayerPrefs.SetString("username", PlayerUsername);
        }

        if (GUI.Button(new Rect(120, 120, 150, 30), "Close Connection"))
        {
            //Debug.Log ("Closing");

            socket.Close();
        }


//spawn player
        if (GUI.Button(new Rect(220, 120, 150, 30), "spawnplayer"))
        {
            Vector3 pos = new Vector3(20.0f, 0.0f, 20.0f);
            //Instantiate(prefabPlayer, pos, Quaternion.identity);

            Instantiate(prefabPlayer, pos, Quaternion.identity);
        }
    }    //end gui