public static bool Start(Action fnConexion, Action fnEnd, Dictionary <string, Action <object> > handlers)
    {
        if (handlers.Count == 0)
        {
            return(false);
        }

        Debug.Log("Comienza initSocketio()");
        try
        {
            IO.Options opciones = new IO.Options();
            opciones.ExtraHeaders.Add("jwt", UserDataScript.getInfo("token"));
            opciones.ExtraHeaders.Add("operacion", op);

            foreach (KeyValuePair <string, string> entry in args)
            {
                opciones.ExtraHeaders.Add(entry.Key, entry.Value);
            }

            socket = IO.Socket(ENDPOINT, opciones);

            foreach (KeyValuePair <string, Action <object> > entry in handlers)
            {
                socket.On(entry.Key, entry.Value);
            }

            socket.On(QSocket.EVENT_DISCONNECT, (reason) => { Debug.Log("Disconnected: " + reason + " \n" + Environment.StackTrace); PlayersDataScript.eliminarPartida();  fnEnd(); });
            socket.On(QSocket.EVENT_RECONNECT, () => { Debug.Log("Reconnected"); });
            socket.On(QSocket.EVENT_CONNECT, () => { Debug.Log("Connected"); fnConexion(); });
        }catch (Exception e) {
            Debug.Log("Exception: " + e);
        }
        return(true);
    }
Example #2
0
    void Start()
    {
        Debug.Log("start");
        socket = IO.Socket("http://localhost:3000");

        socket.On(QSocket.EVENT_CONNECT, () => {
            Debug.Log("Connected");
            socket.Emit("chat", "test");
        });

        socket.On("chat", data => {
            Debug.Log("data : " + data);
        });
    }
 public static void AddHandler(string evento, Action <object> handler)
 {
     if (socket != null)
     {
         socket.On(evento, handler);
     }
 }
    void Start()
    {
        List <ObstacleInfo> map = new List <ObstacleInfo>();

        foreach (GameObject wall in GameObject.FindGameObjectsWithTag("Obstacle"))
        {
            ObstacleInfo obs = new ObstacleInfo();
            obs.x      = wall.transform.position.x;
            obs.y      = wall.transform.position.z;
            obs.width  = wall.transform.localScale.x;
            obs.length = wall.transform.localScale.z;
            map.Add(obs);
        }
        //Debug.Log(JsonConvert.SerializeObject(map));
        Debug.Log("start");
        socket = IO.Socket("http://127.0.0.1:5000/");

        socket.On(QSocket.EVENT_CONNECT, () => {
            Debug.Log("Connected");
            socket.Emit("generate_new_player", @"{""uid"":0}");
        });
        socket.On("error", () => {
            Debug.Log("error disconnect");
            socket.Disconnect();
        });
        socket.On("stub", data => {
            //Debug.Log("stub data");
            //Debug.Log("stub data : " + data);
        });
        socket.On("uid", data => {
            uidget = JsonConvert.DeserializeObject <UIDAnswer>(data.ToString());
            id     = uidget.uid;
        });
        socket.On("update", data => {
            //Debug.Log("update data : " + data);
            PlayerList list = JsonConvert.DeserializeObject <PlayerList>(data.ToString());
            Vector3 pos     = new Vector3();
            pos.x           = list.players[0].x;
            pos.y           = 0.5f;
            pos.z           = list.players[0].y;
            playerMotor.UpdatePosition(pos);
        });
    }
    // Start is called before the first frame update
    void Start()
    {
        socket = null;
        socket = IO.Socket("http://localhost:9090");

        socket.On(QSocket.EVENT_CONNECT, () => {
            Debug.Log("EVENT_CONNECT");
            socket.Emit("unity-start", ": )");
        });
        socket.On(QSocket.EVENT_DISCONNECT, () => {
            Debug.Log("EVENT_DISCONNECT");
            this.socket.Connect();
        });

        socket.On("unity-center", (data) => {
            float value = float.Parse(data.ToString());
            //Debug.Log("center:"+ value);

            if (value > 0.7f)
            {
                this.posID = 0;
            }
        });

        socket.On("unity-left", (data) => {
            float value = float.Parse(data.ToString());
            //Debug.Log("left:" + value);
            if (value > 0.7f)
            {
                this.posID = 1;
            }
        });

        socket.On("unity-right", (data) => {
            float value = float.Parse(data.ToString());
            //Debug.Log("right:" + value);
            if (value > 0.7f)
            {
                this.posID = 2;
            }
        });
    }
Example #6
0
    private void setupSocket()
    {
        socket = IO.Socket(Constants.Server.SERVER_URL);

        // this happens on connect
        socket.On(QSocket.EVENT_CONNECT, () =>
        {
            Debug.Log("Connected to server");
            // The "hello" event is how a user first contacts the server to connect to a game
            if (userInfo.state != GameManager.GameState.AwaitingOpponentCommands)
            {
                socket.Emit("hello", JsonUtility.ToJson(userInfo.exportConnectToServerRequiredInfo()));
            }
            else
            {
                socket.Emit("submittingTurn", JsonUtility.ToJson(userInfo.exportTurnInfo()));
            }
        });

        // Server sends an pairing event if the player successfully connects
        // Justification for change: in multigames, won't really know what player number you are until game starts
        socket.On("pairing", (data) =>
        {
            Debug.Log(data + " received from pairing");
        });

        // We know a game is found when the server sends the first "gameplay" event
        // We retreive the opponent info from this
        socket.On("gameplay", (data) =>
        {
            if (gameStarted == false)
            {
                gameStarted           = true;
                opponentInfo          = JsonUtility.FromJson <UserInfo>(data.ToString());
                userInfo.playerNumber = (opponentInfo.playerNumber == 1) ? 2 : 1;
                Debug.Log("Player " + userInfo.playerNumber
                          + " sees that the game has started");
            }
        });

        // We receive opponent's commands from the server
        socket.On("receiveTurn", (data) =>
        {
            TurnInfo opponentTurnInfo = JsonUtility.FromJson <TurnInfo>(data.ToString());
            Debug.Log("Player " + userInfo.playerNumber +
                      " has received the opponent's turn data");
            if (opponentTurnInfo.commandsUpdated != null &&
                !String.Equals(opponentTurnInfo.commandsUpdated, opponentInfo.commandsUpdated))
            {
                opponentInfo.setCommands(opponentTurnInfo.commands, opponentTurnInfo.commandsUpdated);
            }
        });

        // There is an error. End game.
        socket.On("client error", (data) =>
        {
            gameEnded = true;
            Debug.Log(data.ToString());
            Destroy();
        });

        // End the current game.
        // Data should contain an empty string uder current implementation
        socket.On("endGameConfirmation", (data) =>
        {
            gameEnded = true;
            Debug.Log("Player " + userInfo.playerNumber +
                      " has received the instruction to end the game");
            Destroy();
        });

        // this happens on disconnect
        socket.On(QSocket.EVENT_DISCONNECT, () =>
        {
            Debug.Log("Disconnected from server");
        });
    }