private async Task ListenForConnections()
        {
            if (this.listener.IsStarted)
            {
                try
                {
                    while (true)
                    {
                        WebSocket websocket = await this.listener
                                              .AcceptWebSocketAsync(CancellationToken.None)
                                              .ConfigureAwait(false);

                        if (websocket != null)
                        {
                            Task.Run(() => HandleMessage(websocket));
                        }
                    }
                }

                catch (Exception ex)
                {
                    if (ErrorThrown != null)
                    {
                        ErrorThrown.Invoke(null, ex);
                    }
                }
            }
        }
        private async Task HandleMessage(WebSocket websocket)
        {
            try
            {
                if (ClientConnected != null)
                {
                    ClientConnected.Invoke(websocket);
                }

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

                    //Console.WriteLine("Websocket: Got message.");
                    if (message != null &&
                        MessageReceived != null)
                    {
                        MessageReceived.Invoke(websocket, message);
                    }
                }

                if (ClientDisconnected != null)
                {
                    ClientDisconnected.Invoke(websocket);
                }
            }

            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex);
                if (ErrorThrown != null)
                {
                    ErrorThrown.Invoke(websocket, ex);
                }
            }

            finally
            {
                websocket.Dispose();
            }
        }
Example #3
0
File: Node.cs Project: Nucs/nlib
        public bool Send <T>(string packetName, T o)
        {
            if (Node.This == this)
            {
                throw new InvalidOperationException("Can't send data to self.");
            }

            var conn = this.Connect;

            if (conn == null)
            {
                return(false);
            }
            try {
                conn.SendObject(packetName, o);
            } catch (Exception e) {
                ErrorThrown?.Invoke(e, "Error during sending packet to node");
                return(false);
            }
            return(true);
        }
Example #4
0
File: Node.cs Project: Nucs/nlib
        public bool Send <T>(T o)
        {
            if (Node.This == this)
            {
                throw new InvalidOperationException("Can't send data to self.");
            }

            var conn = this.Connect;

            if (conn == null)
            {
                return(false);
            }
            try {
                conn.SendObject("data" + this.GetHashCode(), new Data <T>(This.GetHashCode(), o));
            } catch (Exception e) {
                ErrorThrown?.Invoke(e, "Error during sending data to node");
                return(false);
            }
            return(true);
        }
Example #5
0
 private void OnError(Exception exception)
 {
     Logger.Error(exception);
     ErrorThrown?.Invoke(this, new ErrorEventArgs(exception));
 }