SendNetworkMessage() public method

public SendNetworkMessage ( Message message, SendTo sendTo, NetIncomingMessage netMessage = null, NetDeliveryMethod deliveryMethod = NetDeliveryMethod.ReliableOrdered ) : void
message Message
sendTo SendTo
netMessage Lidgren.Network.NetIncomingMessage
deliveryMethod NetDeliveryMethod
return void
Example #1
0
        private void SendPlayerHookReleased()
        {
            MultiplayerManager mInstance = MultiplayerManager.instance;

            int playerID = GetComponent <Player>().m_playerId;

            MessagePlayerHookReleased message = new MessagePlayerHookReleased();

            message.playerId = playerID;
            mInstance.SendNetworkMessage(message, SendTo.Others);
        }
Example #2
0
        private void SendPlayerHookUsed(Vector3 hookPosition)
        {
            MultiplayerManager mInstance = MultiplayerManager.instance;

            int playerID = GetComponent <Player>().m_playerId;

            MessagePlayerHookUsed message = new MessagePlayerHookUsed();

            message.playerId     = playerID;
            message.hookPosition = hookPosition;
            mInstance.SendNetworkMessage(message, SendTo.Others);
        }
Example #3
0
        void Update()
        {
            NetIncomingMessage msg;

            while (m_server != null && (msg = m_server.ReadMessage()) != null)
            {
                switch (msg.MessageType)
                {
                case NetIncomingMessageType.VerboseDebugMessage:
                case NetIncomingMessageType.DebugMessage:
                    Debug.Log(msg.ReadString());
                    break;

                case NetIncomingMessageType.WarningMessage:
                    Debug.LogWarning(msg.ReadString());
                    break;

                case NetIncomingMessageType.ErrorMessage:
                    Debug.LogError(msg.ReadString());
                    break;

                case NetIncomingMessageType.StatusChanged:
                    NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();

                    string reason = msg.ReadString();
                    Debug.Log(NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier) + " " + status + ": " + reason);

                    if (status == NetConnectionStatus.Connected)
                    {
                        Debug.Log("Remote connected: " + msg.SenderConnection.RemoteHailMessage.ReadString());

                        NetOutgoingMessage om = m_server.CreateMessage();

                        om.Write((int)MessageFunction.SpawnAllPlayer);
                        om.Write(m_players.Count);

                        foreach (var player in m_players)
                        {
                            om.Write(player.ID);
                            om.Write(player.Name);
                        }
                        m_server.SendMessage(om, msg.SenderConnection, NetDeliveryMethod.ReliableOrdered, 0);
                    }
                    else if (status == NetConnectionStatus.Disconnected)
                    {
                        MultiplayerManager mInstance = MultiplayerManager.instance;

                        var player = m_players.FirstOrDefault(x => x.OwnerConnection == msg.SenderConnection);
                        if (player != null)
                        {
                            MessageRemovePlayer message = new MessageRemovePlayer();
                            message.playerId = player.ID;
                            mInstance.SendNetworkMessage(message, SendTo.All);
                        }
                    }
                    break;

                case NetIncomingMessageType.Data:
                    HandleSeverMessage(msg);
                    break;

                case NetIncomingMessageType.UnconnectedData:
                    var msgType = (UnConnectedMessageFunction)msg.ReadInt32();
                    switch (msgType)
                    {
                    case UnConnectedMessageFunction.PingServerForPlayerCount:
                        NetOutgoingMessage returnMessage = m_server.CreateMessage();
                        returnMessage.Write((int)UnConnectedMessageFunction.PingServerForPlayerCount);
                        returnMessage.Write(m_players.Count);
                        m_server.SendUnconnectedMessage(returnMessage, msg.SenderEndPoint);
                        break;

                    case UnConnectedMessageFunction.PingServerForMapId:
                        NetOutgoingMessage returnMessage2 = m_server.CreateMessage();
                        returnMessage2.Write((int)UnConnectedMessageFunction.PingServerForMapId);
                        returnMessage2.Write(m_players.Count);
                        returnMessage2.Write(m_saveWorkshopID);
                        m_server.SendUnconnectedMessage(returnMessage2, msg.SenderEndPoint);
                        break;
                    }

                    break;

                default:
                    Debug.LogWarning("Unhandled type: " + msg.MessageType);
                    break;
                }
                m_server.Recycle(msg);
            }

            while (m_client != null && (msg = m_client.ReadMessage()) != null)
            {
                switch (msg.MessageType)
                {
                case NetIncomingMessageType.VerboseDebugMessage:
                case NetIncomingMessageType.DebugMessage:
                case NetIncomingMessageType.WarningMessage:
                case NetIncomingMessageType.ErrorMessage:
                    Debug.Log(msg.ReadString());
                    break;

                case NetIncomingMessageType.StatusChanged:
                    if (msg.ReadString() == "=Failed".Trim())
                    {
                        Debug.Log("It failed");
                        m_failedToConnect = true;
                    }
                    break;

                case NetIncomingMessageType.Data:
                    HandleClientMessage(msg);
                    break;

                default:
                    Debug.LogWarning("Unhandled type: " + msg.MessageType);
                    break;
                }
                m_client.Recycle(msg);
            }
        }