private void Initialize() { webSocketServer.ImmediateReconnectListeners = false; webSocketServer.ProtocolFactory = WebSocketsSelectorProcessor.Default; webSocketServer.ConnectionTimeoutSeconds = mainClient.Config.WebServerConfig.WebSocketTimeout / 1000; webSocketServer.Received += message => { try { var messageText = (string)message.Value; if (messageText == null) { return; } var requestObject = JObject.Parse(messageText); var requestType = (WebGuiRequestType)requestObject["requestType"].Value <int>(); switch (requestType) { case WebGuiRequestType.Disconnect: { mainClient.Disconnect(); } break; case WebGuiRequestType.Initialize: { message.Connection.Send(message.Context, JsonConvert.SerializeObject(new { eventType = WebGuiEventType.Initialization, eventData = new { networkInfo = new { state = mainClient.DnmpClient.CurrentStatus, selfClientId = mainClient.DnmpClient.SelfClient?.Id ?? 0xFFFF, clients = mainClient.DnmpClient.CurrentStatus == DnmpClient.ClientStatus.Connected && mainClient.DnmpClient.SelfClient != null ? mainClient.DnmpClient.ClientsById.Concat(new[] { new KeyValuePair <ushort, DnmpNode>( mainClient.DnmpClient.SelfClient.Id, mainClient.DnmpClient.SelfClient) }) .Select(x => new ClientJsonData(x.Value, mainClient)) : null, config = mainClient.Config } } }, new ConfigJsonConverter())); BroadcastNetworkList(); } break; default: throw new ArgumentOutOfRangeException(); } } catch (Exception e) { logger.Debug(e, "Exception on receiving message from WebSocket"); } }; mainClient.DnmpClient.OnConnected += () => BroadcastStatusChange(DnmpClient.ClientStatus.Connected); mainClient.DnmpClient.OnDisconnected += () => BroadcastStatusChange(DnmpClient.ClientStatus.NotConnected); mainClient.DnmpClient.OnConnectionTimeout += () => BroadcastStatusChange(DnmpClient.ClientStatus.NotConnected); mainClient.DnmpClient.OnClientConnected += clientId => { if (clientId == mainClient.DnmpClient.SelfClient.Id) { return; } mainClient.NetworkManager.AddEndPoint(mainClient.CurrentNetworkId, (RealIPEndPoint)mainClient.DnmpClient.ClientsById[clientId].EndPoint); mainClient.NetworkManager.SaveNetworks(); BroadcastClientConnect(mainClient.DnmpClient.ClientsById[clientId]); BroadcastNetworkList(); }; mainClient.DnmpClient.OnClientDisconnected += BroadcastClientDisconnect; mainClient.DnmpClient.OnClientParentChange += (clientId, newParentId, oldParentId) => BroadcastClientUpdate(clientId == mainClient.DnmpClient.SelfClient.Id ? mainClient.DnmpClient.SelfClient : mainClient.DnmpClient.ClientsById[clientId]); }