/// <summary> /// Sends data to a specific group of connected users /// </summary> /// <param name="group"></param> /// <param name="opcode"></param> /// <param name="obj"></param> public void SendToGroup(List <NetworkClient> clients, short opcode, object obj) { if (clients.Count > 0) { lock (clients) { foreach (var client in clients) { if (client.IsConnected()) { //Create a internal data structure var packet = new Packet { opcode = opcode, data = JPacketBuilder.Serialize(obj) }; //Serialize all the packet data var payload = JPacketBuilder.Serialize(packet); //Now convert to a buffer byte byte[] byteData = Encoding.ASCII.GetBytes(payload); Console.WriteLine($"Sending {byteData.Length} bytes to the client id " + client.connectionId); // Begin sending the data to the remote device. client.Socket.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client); } } } } }
/// <summary> /// Sends data to all the connected sockets /// </summary> /// <param name="opcode"></param> /// <param name="obj"></param> public void SendToAll(short opcode, object obj) { for (var i = 0; i < this._connectedSockets.Count; i++) { var user = this._connectedSockets[i]; if (user.IsConnected()) { //Create a internal data structure var packet = new Packet { opcode = opcode, data = JPacketBuilder.Serialize(obj) }; //Serialize all the packet data var payload = JPacketBuilder.Serialize(packet); //Now convert to a buffer byte byte[] byteData = Encoding.ASCII.GetBytes(payload); Console.WriteLine($"Sending {byteData.Length} bytes to the client id " + user.connectionId); // Begin sending the data to the remote device. user.Socket.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), user); } } }