/// <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);
                }
            }
        }
        /// <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);
                        }
                    }
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Sends an object with an opcode identifier to the remote server
        /// </summary>
        /// <param name="opcode"></param>
        /// <param name="data"></param>
        public void Send(short opcode, object data)
        {
            if (!this.Connection.IsConnected())
            {
                return;
            }

            //Create a internal data structure
            var packet = new Packet
            {
                opcode = opcode,
                data   = JPacketBuilder.Serialize(data)
            };

            //Serialize all the packet data
            var payload = JPacketBuilder.Serialize(packet);

            Debug.Log("Payload = " + payload);
            //Now convert to a buffer byte
            byte[] byteData = Encoding.ASCII.GetBytes(payload);

            Debug.Log($"Sending {byteData.Length} bytes to the server");

            // Begin sending the data to the remote device.
            this.Client.BeginSend(byteData, 0, byteData.Length, 0,
                                  new AsyncCallback(SendCallback), this.Client);
        }