private void OnClientReady(WebSocketClient client, string name, string color) { GameObject playerObject = physicsWorld.CreateObject(name, 2, color); client.SetPlayerObject(playerObject); playerObject.SetIsPlayerObject(true); foreach (GameObject gameObject in physicsWorld.GetGameObject()) { SendMessageToClient(client, ParameterMap.Stringify("type", "3", "id", gameObject.GetIdentifier(), "x", gameObject.GetPosXAsString(), "y", gameObject.GetPosYAsString(), "size", gameObject.GetSizeAsString(), "name", gameObject.GetName(), "color", gameObject.GetColor())); } if (client.GetPlayerObject() != null) { SendMessageToClient(client, ParameterMap.Stringify("type", "2", "id", client.GetPlayerObject().GetIdentifier())); lock (clients) { foreach (WebSocketClient otherClient in clients) { if (otherClient != client) { SendMessageToClient(otherClient, ParameterMap.Stringify("type", "3", "id", client.GetPlayerObject().GetIdentifier(), "x", client.GetPlayerObject().GetPosXAsString(), "y", client.GetPlayerObject().GetPosYAsString(), "size", client.GetPlayerObject().GetSizeAsString(), "name", client.GetPlayerObject().GetName(), "color", client.GetPlayerObject().GetColor())); } } } } }
private void OnClientValidMessageReceived(Dictionary <string, string> parameterMap, WebSocketClient client) { int type; if (Int32.TryParse(parameterMap["type"], out type)) { if (type != 1) { //Console.WriteLine("Received message from " + client.GetSocketAddress() + ": " + ParameterMap.Stringify(parameterMap)); } switch (type) { case 0: OnClientReady(client, parameterMap["name"], parameterMap["color"]); break; case 1: { float x; float y; if (Single.TryParse(parameterMap["x"], NumberStyles.Float, CultureInfo.InvariantCulture, out x)) { if (Single.TryParse(parameterMap["y"], NumberStyles.Float, CultureInfo.InvariantCulture, out y)) { OnClientPositionUpdateReceived(client, x, y); } } break; } } } }
public void Start(int port) { if (!isRunning) { isRunning = true; clients = new List <WebSocketClient>(); physicsWorld = new PhysicsWorld(PublishObjectSpawnedToParent, PublishObjectDespawnedToParent, PublishObjectUpdatedToParent); server = new WebSocketServer("ws://0.0.0.0:" + port); Spawner spawner = physicsWorld.CreateSpawner(GameObject.Create(" ", 1, "#fff"), 20, 500); physicsWorld.Start(); spawner.Start(); server.ListenerSocket.NoDelay = true; server.Start(clientConnection => { clientConnection.OnOpen = () => { WebSocketClient client = new WebSocketClient(clientConnection); lock (clients) { clients.Add(client); } OnClientConnected(client); }; clientConnection.OnClose = () => { WebSocketClient client = null; lock (clients) { clients.ForEach((otherClient) => { if (otherClient.GetConnection() == clientConnection) { client = otherClient; } }); if (client != null) { clients.Remove(client); } } OnClientDisconnected(client); }; clientConnection.OnMessage = message => { WebSocketClient client = null; lock (clients) { clients.ForEach((otherClient) => { if (otherClient.GetConnection() == clientConnection) { client = otherClient; } }); } OnClientMessageReceived(client, message); }; }); } }
private void OnClientConnected(WebSocketClient client) { }
private void SendMessageToClient(WebSocketClient client, string message) { client.GetConnection().Send(message); //Console.WriteLine("Sent message to " + client.GetSocketAddress() + ": " + message); }