本文整理汇总了C#中Lidgren.Network.NetServer.SendUnconnectedMessage方法的典型用法代码示例。如果您正苦于以下问题:C# NetServer.SendUnconnectedMessage方法的具体用法?C# NetServer.SendUnconnectedMessage怎么用?C# NetServer.SendUnconnectedMessage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Lidgren.Network.NetServer
的用法示例。
在下文中一共展示了NetServer.SendUnconnectedMessage方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
IPEndPoint masterServerEndpoint = NetUtility.Resolve("localhost", CommonConstants.MasterServerPort);
NetPeerConfiguration config = new NetPeerConfiguration("game");
config.SetMessageTypeEnabled(NetIncomingMessageType.NatIntroductionSuccess, true);
config.Port = 14242;
NetServer server = new NetServer(config);
server.Start();
Console.WriteLine("Server started; waiting 5 seconds...");
System.Threading.Thread.Sleep(5000);
var lastRegistered = -60.0f;
while(Console.KeyAvailable == false || Console.ReadKey().Key != ConsoleKey.Escape)
{
// (re-)register periodically with master server
if (NetTime.Now > lastRegistered + 60)
{
// register with master server
NetOutgoingMessage regMsg = server.CreateMessage();
regMsg.Write((byte)MasterServerMessageType.RegisterHost);
IPAddress mask;
IPAddress adr = NetUtility.GetMyAddress(out mask);
regMsg.Write(server.UniqueIdentifier);
regMsg.Write(new IPEndPoint(adr, 14242));
Console.WriteLine("Sending registration to master server");
server.SendUnconnectedMessage(regMsg, masterServerEndpoint);
lastRegistered = (float)NetTime.Now;
}
NetIncomingMessage inc;
while ((inc = server.ReadMessage()) != null)
{
switch (inc.MessageType)
{
case NetIncomingMessageType.VerboseDebugMessage:
case NetIncomingMessageType.DebugMessage:
case NetIncomingMessageType.WarningMessage:
case NetIncomingMessageType.ErrorMessage:
Console.WriteLine(inc.ReadString());
break;
}
}
System.Threading.Thread.Sleep(1);
}
Console.ReadKey();
}
示例2: MGServer_DoWork
private void MGServer_DoWork (object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
NetPeerConfiguration config = new NetPeerConfiguration (applicationIdentifier);
config.EnableMessageType (NetIncomingMessageType.DiscoveryRequest);
config.EnableMessageType (NetIncomingMessageType.DiscoveryResponse);
config.EnableMessageType (NetIncomingMessageType.NatIntroductionSuccess);
if (availableSession == null)
config.Port = port;
// create and start server
peer = new NetServer (config);
peer.Start ();
myLocalAddress = GetMyLocalIpAddress ();
IPAddress adr = IPAddress.Parse (myLocalAddress);
myLocalEndPoint = new IPEndPoint (adr, port);
// force a little wait until we have a LocalGamer otherwise things
// break. This is the first item in the queue so it shouldnt take long before we
// can continue.
while (session.LocalGamers.Count <= 0)
{
Thread.Sleep(10);
}
if (availableSession != null) {
if (!this.online) {
peer.Connect (availableSession.EndPoint);
} else {
RequestNATIntroduction (availableSession.EndPoint, peer);
}
} else {
if (this.online) {
IPAddress ipaddr = NetUtility.Resolve (masterServer);
if (ipaddr != null) {
m_masterServer = new IPEndPoint (ipaddr, masterserverport);
LocalNetworkGamer localMe = session.LocalGamers [0];
NetOutgoingMessage om = peer.CreateMessage ();
om.Write ((byte)0);
om.Write (session.AllGamers.Count);
om.Write (localMe.Gamertag);
om.Write (session.PrivateGamerSlots);
om.Write (session.MaxGamers);
om.Write (localMe.IsHost);
om.Write (myLocalEndPoint);
om.Write (peer.Configuration.AppIdentifier);
// send up session properties
int[] propertyData = new int[session.SessionProperties.Count * 2];
NetworkSessionProperties.WriteProperties (session.SessionProperties, propertyData);
for (int x = 0; x < propertyData.Length; x++) {
om.Write (propertyData [x]);
}
peer.SendUnconnectedMessage (om, m_masterServer); // send message to peer
} else {
throw new Exception ("Could not resolve live host");
}
}
}
// run until we are done
do {
NetIncomingMessage msg;
while ((msg = peer.ReadMessage ()) != null) {
switch (msg.MessageType) {
case NetIncomingMessageType.UnconnectedData :
break;
case NetIncomingMessageType.NatIntroductionSuccess:
#if !WINDOWS_PHONE
Game.Instance.Log("NAT punch through OK " + msg.SenderEndpoint);
#endif
peer.Connect (msg.SenderEndpoint);
break;
case NetIncomingMessageType.DiscoveryRequest:
//
// Server received a discovery request from a client; send a discovery response (with no extra data attached)
//
// Get the primary local gamer
LocalNetworkGamer localMe = session.LocalGamers [0];
NetOutgoingMessage om = peer.CreateMessage ();
om.Write (session.RemoteGamers.Count);
om.Write (localMe.Gamertag);
om.Write (session.PrivateGamerSlots);
om.Write (session.MaxGamers);
om.Write (localMe.IsHost);
int[] propertyData = new int[session.SessionProperties.Count * 2];
NetworkSessionProperties.WriteProperties (session.SessionProperties, propertyData);
for (int x = 0; x < propertyData.Length; x++) {
om.Write (propertyData [x]);
}
//.........这里部分代码省略.........
示例3: Main
static void Main(string[] args)
{
IPEndPoint masterServerEndpoint = NetUtility.Resolve("localhost", CommonConstants.MasterServerPort);
NetPeerConfiguration config = new NetPeerConfiguration("game");
config.SetMessageTypeEnabled(NetIncomingMessageType.NatIntroductionSuccess, true);
config.Port = 14242;
NetServer server = new NetServer(config);
server.Start();
Console.WriteLine("Server started; waiting 5 seconds...");
System.Threading.Thread.Sleep(5000);
var lastRegistered = -60.0f;
while(Console.KeyAvailable == false || Console.ReadKey().Key != ConsoleKey.Escape)
{
// (re-)register periodically with master server
if (NetTime.Now > lastRegistered + 60)
{
// register with master server
NetOutgoingMessage regMsg = server.CreateMessage();
regMsg.Write((byte)MasterServerMessageType.RegisterHost);
IPAddress mask;
IPAddress adr = NetUtility.GetMyAddress(out mask);
regMsg.Write(server.UniqueIdentifier);
regMsg.Write(new IPEndPoint(adr, 14242));
Console.WriteLine("Sending registration to master server");
server.SendUnconnectedMessage(regMsg, masterServerEndpoint);
lastRegistered = (float)NetTime.Now;
}
NetIncomingMessage inc;
while ((inc = server.ReadMessage()) != null)
{
switch (inc.MessageType)
{
case NetIncomingMessageType.VerboseDebugMessage:
case NetIncomingMessageType.DebugMessage:
case NetIncomingMessageType.WarningMessage:
case NetIncomingMessageType.ErrorMessage:
Console.WriteLine(inc.ReadString());
break;
case NetIncomingMessageType.Data:
// incoming chat message from a client
string chat = inc.ReadString();
Console.WriteLine(chat);
//Output("Broadcasting '" + chat + "'");
// broadcast this to all connections, except sender
List<NetConnection> all = server.Connections; // get copy
all.Remove(inc.SenderConnection);
if (all.Count > 0) {
NetOutgoingMessage om = server.CreateMessage();
om.Write(NetUtility.ToHexString(inc.SenderConnection.RemoteUniqueIdentifier) + " said: " + chat);
server.SendMessage(om, all, NetDeliveryMethod.ReliableOrdered, 0);
}
break;
}
}
System.Threading.Thread.Sleep(1);
}
Console.ReadKey();
}