public static void RegisterSubscribe(string subscript_key, NetServer svr) { try { Log.Information("sub {0}", subscript_key); ISubscriber sub = Cache.Instance.cache.GetSubscriber(); sub.Subscribe(subscript_key, (channel, message) => { try { Log.Information($"redis msg {message}"); var msg = svr.CreateMessage(); msg.Write((string)message); svr.SendUnconnectedToSelf(msg, true); } catch (Exception ex) { Log.Error($"redis sub callback error {ex.ToString()}"); } }); } catch (Exception ex) { Log.Error($"redis sub error {ex.ToString()}"); } }
static void OnConnectionApproval() { // Read the first byte of the packet // ( Enums can be casted to bytes, so it be used to make bytes human readable ) if (inc.ReadByte() == (byte)Packets.Type.LOGIN) { Console.WriteLine("Incoming LOGIN"); // Approve clients connection ( Its sort of agreenment. "You can be my client and i will host you" ) inc.SenderConnection.Approve(); } clients.Add(new Messenger(inc.ToString(), inc.SenderConnection)); // Create message, that can be written and sent NetOutgoingMessage outmsg = Server.CreateMessage(); // first we write byte outmsg.Write((byte)Packets.Type.MESSAGE); // then int outmsg.Write(clients.Count + " clients connected."); // iterate trought every character ingame foreach (Messenger cl in clients) { // This is handy method // It writes all the properties of object to the packet outmsg.WriteAllProperties(cl); } // Send message/packet to all connections, in reliably order, channel 0 // Reliably means, that each packet arrives in same order they were sent. Its slower than unreliable, but easyest to understand Server.SendMessage(outmsg, inc.SenderConnection, NetDeliveryMethod.ReliableOrdered, 0); // Debug Console.WriteLine("Approved new connection and updated clients list"); }
public void ReadMessages() { NetIncomingMessage message; var stop = false; while (!stop) { message = server.ReadMessage(); Console.Write(""); if (message != null) { switch (message.MessageType) { case NetIncomingMessageType.Data: { Console.WriteLine(NetUtility.ToHexString(message.SenderConnection.RemoteUniqueIdentifier) + " said: "); var data = message.ReadString(); Console.WriteLine(data); List <NetConnection> all = server.Connections; all.Remove(message.SenderConnection); if (all.Count > 0) { NetOutgoingMessage om = server.CreateMessage(); om.Write(NetUtility.ToHexString(message.SenderConnection.RemoteUniqueIdentifier) + " said: " + data); server.SendMessage(om, all, NetDeliveryMethod.ReliableOrdered, 0); } if (data == "exit") { stop = true; } break; } case NetIncomingMessageType.DebugMessage: Console.WriteLine(message.ReadString()); break; case NetIncomingMessageType.StatusChanged: Console.WriteLine(message.SenderConnection.Status); if (message.SenderConnection.Status == NetConnectionStatus.Connected) { clients.Add(message.SenderConnection.Peer); Console.WriteLine("{0} has connected.", message.SenderConnection.Peer.Configuration.LocalAddress); } if (message.SenderConnection.Status == NetConnectionStatus.Disconnected) { clients.Remove(message.SenderConnection.Peer); Console.WriteLine("{0} has disconnected.", message.SenderConnection.Peer.Configuration.LocalAddress); } break; default: Console.WriteLine("Unhandled message type: {message.MessageType}"); break; } server.Recycle(message); } } Console.WriteLine("Shutdown package \"exit\" received. Press any key to finish shutdown"); Console.ReadKey(); }
void discoveryRequest(NetIncomingMessage incomingMessage) { NetOutgoingMessage outgoingMessage = server.CreateMessage("Discovery Response"); server.SendDiscoveryResponse(outgoingMessage, incomingMessage.SenderEndPoint); }
public static void Uodate(Game1 game, GameTime gameTime) { gameLobbyTimer -= (float)gameTime.ElapsedGameTime.Milliseconds; while ((incmsg = Server.ReadMessage()) != null) { switch (incmsg.MessageType) { case NetIncomingMessageType.Data: string header = incmsg.ReadString(); switch (header) { case "connect": { string username = incmsg.ReadString(); game.Window.Title = "" + Player.players.Count + header; outmsg = Server.CreateMessage(); outmsg.Write("connect"); outmsg.Write(gameLobbyTimer); Server.SendMessage(outmsg, Server.Connections, NetDeliveryMethod.ReliableOrdered, 0); } break; case "CharacterSelection": { string username = incmsg.ReadString(); string selectedChar = incmsg.ReadString(); int x = incmsg.ReadInt32(); int y = incmsg.ReadInt32(); Player.players.Add(new Player(username, new Vector2(x, y), selectedChar)); game.Window.Title = "" + Player.players.Count + header; for (int i = 0; i < Player.players.Count; i++) { outmsg = Server.CreateMessage(); outmsg.Write("CharacterSelection"); outmsg.Write(Player.players[i].name); outmsg.Write(Player.players[i].selectedCharacter); outmsg.Write((int)Player.players[i].pos.X); outmsg.Write((int)Player.players[i].pos.Y); Server.SendMessage(outmsg, Server.Connections, NetDeliveryMethod.ReliableOrdered, 0); } } break; case "move": { game.Window.Title = "" + Player.players.Count + header; try { string username = incmsg.ReadString(); float x = incmsg.ReadFloat(); float y = incmsg.ReadFloat(); string direction = incmsg.ReadString(); for (int i = 0; i < Player.players.Count; i++) { if (Player.players[i].name.Equals(username)) { Player.players[i].pos = new Vector2(x, y); Player.players[i].direction = direction; } } } catch { continue; } } break; case "chat": string chatItem = incmsg.ReadString(); outmsg = Server.CreateMessage(); outmsg.Write("chat"); outmsg.Write(chatItem); Server.SendMessage(outmsg, Server.Connections, NetDeliveryMethod.ReliableOrdered, 0); break; case "newshot": string bulletname = incmsg.ReadString(); float velocityX = incmsg.ReadFloat(); float velocityY = incmsg.ReadFloat(); float damage = incmsg.ReadFloat(); foreach (Player p in Player.players) { if (p.name == bulletname) { outmsg = Server.CreateMessage(); outmsg.Write("newshot"); outmsg.Write(p.name); outmsg.Write(p.pos.X + 16); outmsg.Write(p.pos.Y + 24); outmsg.Write(velocityX); outmsg.Write(velocityY); outmsg.Write(damage); outmsg.Write(p.selectedCharacter); Server.SendMessage(outmsg, Server.Connections, NetDeliveryMethod.ReliableOrdered, 0); } } break; case "bulletCollision": string name = incmsg.ReadString(); float Damage = incmsg.ReadFloat(); for (int i = 0; i < Player.players.Count; i++) { if (Player.players[i].name == name) { Console.WriteLine(Damage); Player.players[i].health -= Damage; Console.WriteLine("bullet message receieved"); Console.WriteLine(Player.players[i].health); } } break; } break; } } }