/// <summary> /// Handles incoming websocket events, acts accordingly. /// </summary> /// <param name="sender"></param> /// <param name="response"></param> private void WebSocketDataRecieved(object sender, RoomSocket response) { switch (response.message) { // The user has been added to the room case "allocation": webSocketService.token = response.token; webSocketService.roomId = response.room; GetRoomMedia(); break; // There is a change in the state of the media case "mediaChange": Room.CurrentMedia = response.media; MediaChanged?.Invoke(this, new EventArgs()); break; // A message from a user, that was broadcasted to the room is recieved case "message": Room.AddMessage(response.messageData); MessageRecieved?.Invoke(this, new EventArgs()); break; default: break; } }
public void Setup() { Debug.Log("Setting up Network Manager..."); SocketOptions options = new SocketOptions(); options.AutoConnect = false; options.ConnectWith = BestHTTP.SocketIO.Transports.TransportTypes.WebSocket; string uri = string.Format("{0}/socket.io/", this.serverEndpoint); this.socketManager = new SocketManager(new System.Uri(uri), options); this.roomSocket = new RoomSocket(); this.RoomSocket.Connect(); }
public async void StartLoadingData(int roomId) { ws = new ClientWebSocket(); await ws.ConnectAsync(new Uri(Constants.StreamrWSUrl), CancellationToken.None); string isPrivateMsg = "false"; if (isPrivate) { isPrivateMsg = "true"; } var roomConnectData = "{\"room\":\"" + roomId + "\", \"msg\":\"connect\", \"isPrivate\":" + isPrivateMsg + "}"; await SendData(roomConnectData); ArraySegment <Byte> readbuffer = new ArraySegment <byte>(new Byte[8192]); // While the websocket is open, continue to get updates from server. while (ws.State == WebSocketState.Open) { RoomSocket updateValue = null; try { var result = await ws.ReceiveAsync(readbuffer, CancellationToken.None); var str = System.Text.Encoding.Default.GetString(readbuffer.Array, readbuffer.Offset, result.Count); updateValue = Parse(str); if (updateValue != null) { DataRecieved?.Invoke(this, updateValue); } } catch (TaskCanceledException) { System.Diagnostics.Debug.Write("WebSocket Stopped"); } } }
void FixedUpdate() { RoomSocket roomSocket = NetworkManager.Instance.RoomSocket; this.infoText.text = string.Format("Room: {0}\nHost: {1}\nTime: {2}", roomSocket.RoomID, roomSocket.HostID, roomSocket.GetTime()); }