当前位置: 首页>>代码示例>>C#>>正文


C# NetServer.SendToAll方法代码示例

本文整理汇总了C#中Lidgren.Network.NetServer.SendToAll方法的典型用法代码示例。如果您正苦于以下问题:C# NetServer.SendToAll方法的具体用法?C# NetServer.SendToAll怎么用?C# NetServer.SendToAll使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Lidgren.Network.NetServer的用法示例。


在下文中一共展示了NetServer.SendToAll方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SendMessage

 private static void SendMessage(NetServer server, MsgBase msg)
 {
     NetOutgoingMessage om = server.CreateMessage();
     om.Write(msg.Id);
     msg.W(om);
     server.SendToAll(om, NetDeliveryMethod.Unreliable);
 }
开发者ID:xxy1991,项目名称:cozy,代码行数:7,代码来源:ServerOperat.cs

示例2: Main

        public static void Main(string[] args)
        {
            Console.WriteLine("[SERVER] Testing Lidgren-Network-v3...");
            NetPeerConfiguration serverConfig = new NetPeerConfiguration("test");
            serverConfig.EnableMessageType(NetIncomingMessageType.DiscoveryRequest);
            serverConfig.MaximumConnections = 32;
            serverConfig.Port = 12345;
            NetServer server = new NetServer(serverConfig);
            server.Start();

            SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
            server.RegisterReceivedCallback(new SendOrPostCallback(GotMessage));

            while(true) {
                string msg = Console.ReadLine();

                NetOutgoingMessage sendMsg = server.CreateMessage();
                sendMsg.Write("[SERVER] " + msg);

                server.SendToAll(sendMsg, NetDeliveryMethod.ReliableOrdered);
            }
        }
开发者ID:uwrov,项目名称:teaching,代码行数:22,代码来源:Server.cs

示例3: Main

        static void Main(string[] args)
        {
            // create a configuration for the server
            NetConfiguration config = new NetConfiguration("chatApp");
            config.MaxConnections = 128;
            config.Port = 14242;

            // create server and start listening for connections
            NetServer server = new NetServer(config);
            server.SetMessageTypeEnabled(NetMessageType.ConnectionApproval, true);
            server.Start();

            // create a buffer to read data into
            NetBuffer buffer = server.CreateBuffer();

            // keep running until the user presses a key
            Console.WriteLine("Press ESC to quit server");
            bool keepRunning = true;
            while (keepRunning)
            {
                NetMessageType type;
                NetConnection sender;

                // check if any messages has been received
                while(server.ReadMessage(buffer, out type, out sender))
                {
                    switch (type)
                    {
                        case NetMessageType.DebugMessage:
                            Console.WriteLine(buffer.ReadString());
                            break;
                        case NetMessageType.ConnectionApproval:
                            Console.WriteLine("Approval; hail is " + buffer.ReadString());
                            sender.Approve();
                            break;
                        case NetMessageType.StatusChanged:
                            Console.WriteLine("New status for " + sender + ": " + sender.Status + " (" + buffer.ReadString() + ")");
                            break;
                        case NetMessageType.Data:
                            // A client sent this data!
                            string msg = buffer.ReadString();

                            // send to everyone, including sender
                            NetBuffer sendBuffer = server.CreateBuffer();
                            sendBuffer.Write(sender.RemoteEndpoint.ToString() + " wrote: " + msg);

                            // send using ReliableInOrder
                            server.SendToAll(sendBuffer, NetChannel.ReliableInOrder1);
                            break;
                    }
                }

                // User pressed ESC?
                while (Console.KeyAvailable)
                {
                    ConsoleKeyInfo info = Console.ReadKey();
                    if (info.Key == ConsoleKey.Escape)
                        keepRunning = false;
                }

                Thread.Sleep(1);
            }

            server.Shutdown("Application exiting");
        }
开发者ID:JeffM2501,项目名称:HackSharp,代码行数:65,代码来源:Program.cs

示例4: Main


//.........这里部分代码省略.........
                                    }

                                    foreach (KeyValuePair<long, Client> entry in UserToConnection)
                                    {
                                        //if (entry.Key != inc.SenderConnection.RemoteUniqueIdentifier)
                                        {
                                            NetOutgoingMessage syncClient = server.CreateMessage();

                                            syncClient.Write((byte) PacketTypes.RECEIVEPLAYER);
                                            syncClient.Write(entry.Value.Name);
                                            syncClient.Write(entry.Key);

                                            var buffer1 = entry.Value.Buffer;

                                            syncClient.Write(buffer1.Length);
                                            syncClient.Write(buffer1);
                                            Console.WriteLine(buffer1.Length);

                                            Console.WriteLine("SENT Client Name: {0}| Client ID: {1}", entry.Value.Name,
                                                entry.Key);
                                            Console.WriteLine("TO: {0}", inc.SenderConnection);

                                            server.SendMessage(syncClient, inc.SenderConnection,
                                                NetDeliveryMethod.ReliableOrdered); //CLIENT
                                        }
                                    }

                                    break;

                                case PacketTypes.SENDPOS:
                                    UserToConnection[inc.SenderConnection.RemoteUniqueIdentifier].Score =
                                        Convert.ToInt16(inc.ReadString());
                                    Console.WriteLine("{0}'s score is now: {1}",
                                        UserToConnection[inc.SenderConnection.RemoteUniqueIdentifier].Name,
                                        UserToConnection[inc.SenderConnection.RemoteUniqueIdentifier].Score);
                                    ID = inc.SenderConnection.RemoteUniqueIdentifier;
                                    UpdateScore();

                                    break;

                                case PacketTypes.STARTGAME:
                                    //GOGOGOGO
                                    Console.WriteLine("START THE GAME");
                                    Started = true;

                                    foreach (KeyValuePair<long, Client> entry in UserToConnection)
                                    {
                                        {
                                            NetOutgoingMessage syncClient = server.CreateMessage();
                                            syncClient.Write((byte) PacketTypes.STARTGAME);
                                            syncClient.Write("Go");
                                            syncClient.Write(
                                                UserToConnection[inc.SenderConnection.RemoteUniqueIdentifier].Score);

                                            server.SendMessage(syncClient, entry.Value.Connection,
                                                NetDeliveryMethod.ReliableOrdered);
                                        }
                                    }

                                    break;

                                case PacketTypes.CHATSEND:
                                    string messager = inc.ReadString();

                                    Console.WriteLine("{0} : {1}",
                                        UserToConnection[inc.SenderConnection.RemoteUniqueIdentifier].Name,
                                        messager);

                                    RelayChat(inc.SenderConnection.RemoteUniqueIdentifier, messager);

                                    break;
                            }

                            break;

                        case NetIncomingMessageType.StatusChanged:

                            Console.WriteLine(inc.SenderConnection + " status changed. " + inc.SenderConnection.Status);

                            if (inc.SenderConnection.Status == NetConnectionStatus.Disconnected ||
                                inc.SenderConnection.Status == NetConnectionStatus.Disconnecting)
                            {
                                UserToConnection.Remove(inc.SenderConnection.RemoteUniqueIdentifier);
                                NetOutgoingMessage outmsg = server.CreateMessage();
                                outmsg.WriteVariableInt64(inc.SenderConnection.RemoteUniqueIdentifier);
                                server.SendToAll(outmsg, NetDeliveryMethod.ReliableOrdered);
                            }
                            break;

                        case NetIncomingMessageType.DiscoveryRequest:
                            NetOutgoingMessage discovermsg = server.CreateMessage();
                            discovermsg.Write("Hey I just met you, I'm a server, so address me maybe");
                            Console.WriteLine("WOW ~NETWORKING~");
                            server.SendDiscoveryResponse(discovermsg, inc.SenderEndPoint);
                            break;
                    }
                }
                Thread.Sleep(1);
            }
        }
开发者ID:JamieH,项目名称:SpaceBarGame,代码行数:101,代码来源:Program.cs

示例5: HandleProtocol

        private static void HandleProtocol(NetIncomingMessage inc, byte packetheader, NetServer server)
        {
            switch ((PacketTypes.Headers) packetheader)
            {
                case PacketTypes.Headers.ReadyUpdate:
                    bool playerStatus = Convert.ToBoolean(inc.ReadString());
                    Player thePlayer = LookUpPlayer(inc.SenderConnection.RemoteUniqueIdentifier);

                    Console.WriteLine("Player {0} changed status to {1}", thePlayer.Name, playerStatus);
                    Players[inc.SenderConnection.RemoteUniqueIdentifier].ToggleReady();
                    NetOutgoingMessage msg = server.CreateMessage();
                    msg.Write((byte) PacketTypes.Headers.ReadyUpdate);
                    msg.Write(inc.SenderConnection.RemoteUniqueIdentifier);
                    msg.Write(playerStatus);
                    server.SendToAll(msg, NetDeliveryMethod.ReliableOrdered);
                    break;
                case PacketTypes.Headers.LoggedIn:
                    string username = inc.ReadString();
                    Player newPlayer = NewPlayer(inc, server, username);
                    Console.WriteLine(Players.Count);
                    if (Players.Count == 0)
                    {
                        Console.WriteLine("Sending {0} host packet", newPlayer.Name);
                        NetOutgoingMessage hMessage = server.CreateMessage();
                        hMessage.Write((byte) PacketTypes.Headers.YouAreHost);
                        server.SendMessage(hMessage, inc.SenderConnection, NetDeliveryMethod.ReliableOrdered);
                    }

                    Players.Add(inc.SenderConnection.RemoteUniqueIdentifier, newPlayer);
                    Players[inc.SenderConnection.RemoteUniqueIdentifier].Connection = inc.SenderConnection;
                    break;
                case PacketTypes.Headers.ChatSend:
                    if (Players[inc.SenderConnection.RemoteUniqueIdentifier].Name != _drawer)
                    {
                        NetOutgoingMessage chatMessageRelay = server.CreateMessage();
                        string chatM = inc.ReadString();
                        chatMessageRelay.Write((byte) PacketTypes.Headers.ChatReceive);
                        chatMessageRelay.Write(Players[inc.SenderConnection.RemoteUniqueIdentifier].Name);
                        chatMessageRelay.Write(chatM);
                        if (!_badwordList.Contains(chatM, StringComparer.OrdinalIgnoreCase))
                        {
                            server.SendToAll(chatMessageRelay, NetDeliveryMethod.ReliableOrdered);
                        }
                        if (String.Equals(chatM, _theWord, StringComparison.CurrentCultureIgnoreCase) & chatM != null)
                        {
                            NetOutgoingMessage someoneOne = server.CreateMessage();
                            someoneOne.Write((byte) PacketTypes.Headers.ChatReceive);
                            someoneOne.Write("Server");
                            someoneOne.Write(Players[inc.SenderConnection.RemoteUniqueIdentifier].Name
                                             + " has guessed the correct word being: " + _theWord);
                            server.SendToAll(someoneOne, NetDeliveryMethod.ReliableOrdered);
                            RoundTimer.Stop();
                            StartNewRound(server);
                        }
                    }
                    break;
                case PacketTypes.Headers.PictureUpdate:
                    int r = inc.ReadVariableInt32();
                    int g = inc.ReadVariableInt32();
                    int b = inc.ReadVariableInt32();
                    int x = inc.ReadVariableInt32();
                    int y = inc.ReadVariableInt32();
                    int size = inc.ReadVariableInt32();
                    NetOutgoingMessage msgPicture = server.CreateMessage();
                    msgPicture.Write((byte) PacketTypes.Headers.PictureUpdate);
                    msgPicture.WriteVariableInt32(r);
                    msgPicture.WriteVariableInt32(g);
                    msgPicture.WriteVariableInt32(b);
                    msgPicture.WriteVariableInt32(x);
                    msgPicture.WriteVariableInt32(y);
                    msgPicture.WriteVariableInt32(size);
                    server.SendToAll(msgPicture, NetDeliveryMethod.ReliableOrdered);
                    break;
                case PacketTypes.Headers.StartGame:
                    NetOutgoingMessage msgStart = server.CreateMessage();
                    msgStart.Write((byte) PacketTypes.Headers.StartGame);
                    server.SendToAll(msgStart, NetDeliveryMethod.ReliableOrdered);
                    RoundTimer.Enabled = true;
                    StartNewRound(server);
                    break;
                case PacketTypes.Headers.DrawLine:
                {
                    int r1 = inc.ReadVariableInt32();
                    int g1 = inc.ReadVariableInt32();
                    int b1 = inc.ReadVariableInt32();
                    int x1 = inc.ReadVariableInt32();
                    int y1 = inc.ReadVariableInt32();
                    int size1 = inc.ReadVariableInt32();
                    int x11 = inc.ReadVariableInt32();
                    int y11 = inc.ReadVariableInt32();
                    NetOutgoingMessage msgDrawLine = server.CreateMessage();
                    msgDrawLine.Write((byte) PacketTypes.Headers.DrawLine);
                    msgDrawLine.WriteVariableInt32(r1);
                    msgDrawLine.WriteVariableInt32(g1);
                    msgDrawLine.WriteVariableInt32(b1);
                    msgDrawLine.WriteVariableInt32(x1);
                    msgDrawLine.WriteVariableInt32(y1);
                    msgDrawLine.WriteVariableInt32(size1);
                    msgDrawLine.WriteVariableInt32(x11);
                    msgDrawLine.WriteVariableInt32(y11);
//.........这里部分代码省略.........
开发者ID:JamieH,项目名称:Pictionary,代码行数:101,代码来源:Program.cs

示例6: StartNewRound

        private static void StartNewRound(NetServer server)
        {
            Round theRound = NewRound();
            _drawer = theRound.Drawer.Name;
            _theWord = theRound.Word;
            NetOutgoingMessage newRoundMsg = server.CreateMessage();
            newRoundMsg.Write((byte) PacketTypes.Headers.NewRound);
            newRoundMsg.Write(theRound.Drawer.Name);
            server.SendToAll(newRoundMsg, NetDeliveryMethod.ReliableOrdered);

            NetOutgoingMessage drawerMsg = server.CreateMessage();
            drawerMsg.Write((byte) PacketTypes.Headers.WordMessage);
            drawerMsg.Write(theRound.Word);
            server.SendMessage(drawerMsg, theRound.Drawer.Connection, NetDeliveryMethod.ReliableOrdered);
            _theWord = theRound.Word;
            RoundTimer.Start();
        }
开发者ID:JamieH,项目名称:Pictionary,代码行数:17,代码来源:Program.cs


注:本文中的Lidgren.Network.NetServer.SendToAll方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。