// -------------------------------------------------------------------------------- // The following functions should not do anything other than call other functions // present elsewhere in the code. They exist as an abstraction layer between // server and client: if the implementation of one is changed, then you'll have // to change some function names here -- but the benefit is that you _won't_ have // to go digging through the rest of the code to find all the references in the // other half of the project. // -------------------------------------------------------------------------------- /// <summary> /// Send the current player's command inputs to the server. /// /// This should be called by C# functions within Unity, and it should call /// Socket.IO-related functions within RemoteController. /// </summary> /// <param name="commands">the player's commands, to be sent to the server</param> public void SendPlayerCommands_ToServer(string commands) { // Create a TurnInfo object Debug.Log("Player " + userInfo.playerNumber + " is submitting their turn"); userInfo.setCommands(commands); Debug.Log(userInfo.commandsUpdated); socket.Emit("submittingTurn", JsonUtility.ToJson(userInfo.exportTurnInfo())); }
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); }); }
// Update is called once per frame public void UpdateEvent(float x, float y, float angle, bool shoot) { if (uidget != null) { UpdateSend data = new UpdateSend(); data.id = uidget.uid; data.x = x; data.y = y; data.angle = angle; data.shot = shoot; string res = JsonConvert.SerializeObject(data); socket.Emit("update", res); } }
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; } }); }
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"); }); }