Пример #1
0
        public void RemovePlayerData(string identifier, string key)
        {
            if (string.IsNullOrEmpty(identifier))
            {
                LiveMap.Log(LiveMap.LogLevel.Basic, "Identifier is null or empty.. Cannot remove player data");
                return;
            }
            if (string.IsNullOrEmpty(key))
            {
                LiveMap.Log(LiveMap.LogLevel.Basic, "Cannot remove data from player ({0}) because key is null or empty", identifier);
                return;
            }

            MakeSurePlayerExists(identifier);
            lock (playerData)
            {
                JObject playerObj = (JObject)playerData[identifier];
                if (playerObj[key] != null)
                {
                    if (playerObj.Remove(key))
                    {
                        LiveMap.Log(LiveMap.LogLevel.Basic, "Removed \"{0}\" from player {1}", key, identifier);
                        //PlayerHadBeenUpdated(identifier, playerObj);
                    }
                    else
                    {
                        LiveMap.Log(LiveMap.LogLevel.Basic, "Couldn't remove \"{0}\" from player {1}", key, identifier);
                    }
                }// else = already removed
            }
        }
Пример #2
0
        public void UpdatePlayerData(string identifier, string key, object newData)
        {
            if (string.IsNullOrEmpty(identifier))
            {
                LiveMap.Log(LiveMap.LogLevel.Basic, "Identifier is null or empty. Cannot update player data");
                return;
            }
            if (string.IsNullOrEmpty(key))
            {
                LiveMap.Log(LiveMap.LogLevel.Basic, "Cannot update player ({0}) because key is null or empty", identifier);
                return;
            }

            // Check if `data` is null
            if (newData == null)
            {
                LiveMap.Log(LiveMap.LogLevel.Basic, "Cannot update \"{0}\" for {1} because it's null", key, identifier);
                return;
            }
            LiveMap.Log(LiveMap.LogLevel.All, "Updating player {0}'s \"{1}\"", identifier, key);
            MakeSurePlayerExists(identifier);

            lock (playerData)
            {
                JObject playerObj = (JObject)playerData[identifier];
                playerObj[key]         = JToken.FromObject(newData);
                playerData[identifier] = playerObj;

                //PlayerHadBeenUpdated(identifier, playerObj);
            }

            LiveMap.Log(LiveMap.LogLevel.All, "Updated player {0}'s \"{1}\" to \"{2}\"", identifier, key, newData);
        }
Пример #3
0
        public WebSocketServer(int port)
        {
            WebSocketListenerOptions opts = new WebSocketListenerOptions()
            {
                SubProtocols = new string[] { "text" },
                //NegotiationQueueCapacity = 128,
                ParallelNegotiations      = 16,
                HttpAuthenticationHandler = async(request, response) => {
                    await Task.Delay(0);             // To shut the f*****g IDE up

                    if (LiveMap.accessOrigin == "*") // If they're allowing anyone
                    {
                        return(true);
                    }

                    // Check if the origin is the same as the accessOrigin in CFG file
                    return(request.Headers["Origin"].Equals(LiveMap.accessOrigin, StringComparison.CurrentCultureIgnoreCase));
                }
            };

            opts.Standards.RegisterRfc6455();

            //opts.HttpAuthenticationHandler = OnHttpNegotiationDelegate;

            listener = new WebSocketListener(new System.Net.IPEndPoint(System.Net.IPAddress.Any, port), opts);

            LiveMap.Log(LiveMap.LogLevel.Basic, "Created websocket server");
        }
Пример #4
0
        private void Server_OnConnect(WebSocket ws)
        {
            LiveMap.Log(LiveMap.LogLevel.All, "Socket connection opened at {0}", ws.RemoteEndpoint.ToString());

            if (clients.TryAdd(ws.RemoteEndpoint.ToString(), ws))
            {
                LiveMap.Log(LiveMap.LogLevel.All, "Added client {0} to the client dictionary", ws.RemoteEndpoint.ToString());
            }
            else
            {
                LiveMap.Log(LiveMap.LogLevel.All, "Couldn't add {0} to the client dic", ws.RemoteEndpoint.ToString());
            }
        }
Пример #5
0
        public void UpdateBlip(dynamic blip)
        {
            if (blip == null)
            {
                LiveMap.Log(LiveMap.LogLevel.Basic, "Cannot update blip as it's null");
                return;
            }

            JObject payload = new JObject();

            payload["type"]    = "updateBlip";
            payload["payload"] = ConvertBlip(blip);

            sendQueue.Enqueue(payload);
        }
Пример #6
0
        private void Server_OnDisconnect(WebSocket ws)
        {
            LiveMap.Log(LiveMap.LogLevel.All, "Socket connection was closed at {0}", ws.RemoteEndpoint.ToString());

            WebSocket destory;

            if (clients.TryRemove(ws.RemoteEndpoint.ToString(), out destory))
            {
                destory.CloseAsync().GetAwaiter().GetResult();
                destory.Dispose();
                LiveMap.Log(LiveMap.LogLevel.All, "Removed {0} socket because it disconnected", ws.RemoteEndpoint.ToString());
            }
            else
            {
                LiveMap.Log(LiveMap.LogLevel.All, "Couldn't remove {0} from the clients dic.", ws.RemoteEndpoint.ToString());
            }
        }
Пример #7
0
        private void Server_OnError(WebSocket ws, Exception ex)
        {
            LiveMap.Log(LiveMap.LogLevel.Basic, "Socket error from {0}: {1}", ws == null ? "Unknown" : ws.RemoteEndpoint.ToString(), ex.Message);

            if (ws != null)
            {
                WebSocket destory;
                if (clients.TryRemove(ws.RemoteEndpoint.ToString(), out destory))
                {
                    destory.Dispose();
                    LiveMap.Log(LiveMap.LogLevel.Basic, "Removed {0} socket because of an error: {1}\nInner: {2}", ws.RemoteEndpoint.ToString(), ex.Message, ex.InnerException);
                }
                else
                {
                    LiveMap.Log(LiveMap.LogLevel.Basic, "Couldn't remove {0} from the clients dic.", ws.RemoteEndpoint.ToString());
                }
            }
        }
Пример #8
0
        public void RemovePlayer(string identifier)
        {
            if (string.IsNullOrEmpty(identifier))
            {
                LiveMap.Log(LiveMap.LogLevel.Basic, "Identifier is null or empty. Cannot remove player");
                return;
            }

            bool playerLeftBool = false;

            lock (playerData)
            {
                if (playerData[identifier] != null)
                {
                    if (playerData.Remove(identifier))
                    {
                        playerLeftBool = true;
                        LiveMap.Log(LiveMap.LogLevel.Basic, "Removed player {0}", identifier);
                    }
                    else
                    {
                        LiveMap.Log(LiveMap.LogLevel.Basic, "Couldn't remove player {0}... Seriously, there's something f*****g wrong here...", identifier);
                    }
                }
            }

            LiveMap.Log(LiveMap.LogLevel.All, "Notifying ws clients that a player left? {0}", playerLeftBool);

            if (playerLeftBool)
            {
                JObject payload = new JObject();
                payload["type"]    = "playerLeft";
                payload["payload"] = identifier;

                sendQueue.Enqueue(payload);
            }
        }
Пример #9
0
        public void AddPlayerData(string identifier, string key, object data)
        {
            if (string.IsNullOrEmpty(identifier))
            {
                LiveMap.Log(LiveMap.LogLevel.Basic, "Identifier is null or empty");
                return;
            }
            if (string.IsNullOrEmpty(key))
            {
                LiveMap.Log(LiveMap.LogLevel.Basic, "Cannot add key to player ({0}) because it's null or empty", identifier);
                return;
            }

            if (data == null)
            {
                LiveMap.Log(LiveMap.LogLevel.Basic, "Cannot add \"{1}\" to player ({0}) because it's null.", identifier, key);
                return;
            }

            LiveMap.Log(LiveMap.LogLevel.All, "Adding player {0}'s \"{1}\"", identifier, key);
            MakeSurePlayerExists(identifier);

            lock (playerData)
            {
                JObject playerObj = (JObject)playerData[identifier];

                if (playerObj[key] == null)
                {
                    playerObj.Add(key, JToken.FromObject(data));
                }

                //PlayerHadBeenUpdated(identifier, playerObj);
            }

            LiveMap.Log(LiveMap.LogLevel.Basic, "Added \"{1}\" to player {0} with value of \"{2}\"", identifier, key, data);
        }
Пример #10
0
        private async Task HandleSocket(WebSocket ws)
        {
            try
            {
                if (OnConnect != null)
                {
                    OnConnect.Invoke(ws);
                }

                while (ws.IsConnected)
                {
                    string message = await ws.ReadStringAsync(CancellationToken.None).ConfigureAwait(false);

                    if (message != null && OnMessage != null)
                    {
                        OnMessage.Invoke(ws, message);
                    }
                }

                if (OnDisconnect != null)
                {
                    OnDisconnect.Invoke(ws);
                }
            }catch (Exception e)
            {
                LiveMap.Log(LiveMap.LogLevel.Basic, "Error in HandleSocket:\n{0}\n---Inner:\n{1}", e.Message, e.InnerException);
                if (OnError != null)
                {
                    OnError.Invoke(ws, e);
                }
            }
            finally
            {
                ws.Dispose();
            }
        }
Пример #11
0
 private void Server_OnMessage(WebSocket ws, string msg)
 {
     string[] args = msg.Split(' ');
     LiveMap.Log(LiveMap.LogLevel.Basic, "Recieved message from client {0}:\n\t\"{1}\"", ws.RemoteEndpoint.ToString(), msg);
 }
Пример #12
0
        public async Task SendWebsocketData()
        {
            while (true)
            {
                // Only send the data every .5 seconds
                await Task.Delay(TimeSpan.FromMilliseconds(LiveMap.waitSeconds)).ConfigureAwait(false);

                //LiveMap.Log(LiveMap.LogLevel.All, "Checking send queue");
                List <string> disconnectedClients = new List <string>();
                if (sendQueue.Count != 0)
                {
                    JObject payload;
                    if (sendQueue.TryDequeue(out payload))
                    {
                        foreach (KeyValuePair <string, WebSocket> pair in clients)
                        {
                            string    endpoint = pair.Key;
                            WebSocket ws       = pair.Value;

                            if (!ws.IsConnected)
                            {
                                disconnectedClients.Add(endpoint);
                                continue;
                            }

                            LiveMap.Log(LiveMap.LogLevel.All, "Sending payload of \"{0}\" to {1}", payload["type"], endpoint);

                            await ws.WriteStringAsync(payload.ToString(Newtonsoft.Json.Formatting.None), CancellationToken.None).ConfigureAwait(false);
                        }
                    }
                    else
                    {
                        LiveMap.Log(LiveMap.LogLevel.Basic, "Couldn't get the latest payload to send.");
                    }
                }
                else
                {
                    // No payload in the queue, may as well send player data

                    // Generate the payload
                    JObject payload         = new JObject();
                    JArray  playerDataArray = new JArray();
                    lock (playerData)
                    {
                        foreach (KeyValuePair <string, JToken> data in playerData)
                        {
                            playerDataArray.Add(data.Value);
                        }
                    }

                    if (playerDataArray.Count == 0)
                    {
                        //LiveMap.Log(LiveMap.LogLevel.All, "playerDataArray.Count is 0");
                        continue;
                    }
                    payload["type"]    = "playerData";
                    payload["payload"] = playerDataArray;

                    foreach (KeyValuePair <string, WebSocket> pair in clients)
                    {
                        string    endpoint = pair.Key;
                        WebSocket ws       = pair.Value;

                        if (!ws.IsConnected)
                        {
                            disconnectedClients.Add(endpoint);
                            continue;
                        }

                        //LiveMap.Log(LiveMap.LogLevel.All, "Sending payload of \"{0}\" to {1}", payload["type"], endpoint);
                        await ws.WriteStringAsync(payload.ToString(Newtonsoft.Json.Formatting.None), CancellationToken.None).ConfigureAwait(false);
                    }
                } // end of payload sending.. Time to disconnect clients that are no longer connected

                //LiveMap.Log(LiveMap.LogLevel.All, "Client size: {0}", clients.Count);
                foreach (string endpoint in disconnectedClients)
                {
                    WebSocket des;
                    if (clients.TryRemove(endpoint, out des))
                    {
                        LiveMap.Log(LiveMap.LogLevel.All, "Garbage cleanup.. Removed disconnected client {0}", endpoint);
                        des.Dispose();
                    }
                }
            }
        }