static void SendToAllClients(ServerCommand command, bool retryOnError = false) { try { clients.Where(x => x != null && (x.state == ClientState.Connected || x.state == ClientState.Playing)) .AsParallel().ForAll(x => { x.sendQueue.Enqueue(command); }); } catch (Exception e) { Logger.Instance.Exception("Can't send message to all clients! Exception: " + e); } }
static void SendToAllClients(ServerCommand command, WebSocketServer wss, bool retryOnError = false) { try { clients.Where(x => x != null && (x.state == ClientState.Connected || x.state == ClientState.Playing)) .AsParallel().ForAll(x => { x.sendQueue.Enqueue(command); }); if (wss != null) { wss.WebSocketServices["/"].Sessions.Broadcast(JsonConvert.SerializeObject(command)); } } catch (Exception e) { Logger.Instance.Exception("Can't send message to all clients! Exception: " + e); } }
public bool SendToClient(ServerCommand command) { if (_client == null || !_client.Connected) { return(false); } string message = JsonConvert.SerializeObject(command, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore }); byte[] buffer = Encoding.UTF8.GetBytes(message); try { _client.GetStream().Write(buffer, 0, buffer.Length); } catch (Exception) { return(false); } return(true); }