// Use this for initialization void Start() { Term.AddCommand("join", "joins a server at localhost:3935", (args) => { StartClient("localhost", 3935); }); Term.AddCommand("join (ip)", "joins a server at given ip and port 3935", (args) => { string ip = args[0]; StartClient(ip, 3935); }); Term.AddCommand("join (ip) (port)", "joins a server at given ip and port", (args) => { string ip = args[0]; int port = 3935; int.TryParse(args[1], out port); StartClient(ip, port); }); Term.AddCommand("host", "host a server at port 3935", (args) => { StartServer(3935); }); Term.AddCommand("host (port)", "host a server at given port", (args) => { int port = 3935; int.TryParse(args[0], out port); StartServer(port); }); }
private bool LaunchServer(int port) { if (NetworkServer.active) { return(false); } Spawner = GetComponent <Spawner>(); ConnectionConfig Config = new ConnectionConfig(); NetworkChannelID.ReliableSequenced = Config.AddChannel(QosType.ReliableSequenced); NetworkChannelID.UnreliableSequenced = Config.AddChannel(QosType.UnreliableSequenced); NetworkChannelID.Unreliable = Config.AddChannel(QosType.Unreliable); NetworkServer.Configure(Config, 10); NetworkServer.Listen(port); Spawner.SyncDB.SetStaticObjectsIDs(); NetworkServer.RegisterHandler(PktType.TextMessage, HandlePacket); NetworkServer.RegisterHandler(PktType.MoveCreature, HandlePacket); NetworkServer.RegisterHandler(PktType.Interact, HandlePacket); NetworkServer.RegisterHandler(PktType.ClientSync, HandlePacket); NetworkServer.RegisterHandler(PktType.Disconnect, HandlePacket); NetworkServer.RegisterHandler(PktType.FailedChecksums, HandlePacket); NetworkServer.RegisterHandler(PktType.InventoryAction, HandlePacket); NetworkServer.RegisterHandler(MsgType.Connect, OnConnected); NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnected); NetworkServer.RegisterHandler(MsgType.Error, OnError); Debug.Log("Server started on port " + port); Term.Println("Server started on port " + port); Term.AddCommand("send (message)", "Howl at the darkness of space. Does it echo though?", (args) => { Term.Println("You: " + args[0]); SendToAll(PktType.TextMessage, new TextMessagePkt("Server: " + args[0])); }); Term.AddCommand("unhost", "Shuts down the server, shut it all down!", (args) => { Term.Println("Unhosting the server."); Shutdown(); }); Syncer = gameObject.AddComponent <ServerSyncer>(); ServerSyncHandler = new ServerSyncHandler(Players); return(true); }
// Built-in handles for some events private void OnConnected(NetworkMessage msg) { Debug.Log("Connected!"); Term.Println("Connected!"); Term.AddCommand("send (message)", "Send a message across the vastness of space and time!", (args) => { Term.Println("You: " + args[0]); NetClient.Send(PktType.TextMessage, new TextMessagePkt("A Client: " + args[0])); }); Term.AddCommand("disconnect", "Disconnect from the server.", (args) => { NetClient.Send(PktType.Disconnect, new DisconnectPkt()); }); }
// console commands (and create client) private void addCommands() { Term.AddCommand(CMD_CONNECT, async(s) => { try { if (client == null) { // Create Discord Client if (!File.Exists(TOKEN_FILE)) { Term.Log("Token file '" + TOKEN_FILE + "' not found.", ConsoleColor.Red); return; } client = new DiscordClient(new DiscordConfig() { Token = File.ReadAllText(TOKEN_FILE), TokenType = TokenType.Bot, DiscordBranch = Branch.Canary, #if DEBUG LogLevel = LogLevel.Debug, #else LogLevel = LogLevel.Info, #endif UseInternalLogHandler = true, AutoReconnect = true }); client.DebugLogger.LogMessageReceived += DebugLogger_LogMessageReceived; client.Ready += Client_Ready; client.SocketOpened += Client_SocketOpened; client.SocketClosed += Client_SocketClosed; client.MessageCreated += Client_MessageCreated; } await client.Connect(); } catch (Exception ex) { Term.Log("Error: " + ex.Message, ConsoleColor.Red); } }, "Connect to server."); // ====================================================================== // Term.AddCommand(CMD_DISCONNECT, async(s) => { if (client == null) { Term.Log("DiscordClient is null.", ConsoleColor.Yellow); return; } Term.Log("Disconnect.."); await client.Disconnect(); }, "Disonnect from server."); // ====================================================================== // Term.AddCommand(CMD_EXIT, async(s) => { if (Connected) { Term.Log("Disconnect.."); await client.Disconnect(); } stop = true; }, "Close this program."); // ====================================================================== // Term.AddCommand(CMD_CLEAR, (s) => { Console.Clear(); }, "Clear console."); // ====================================================================== // Term.AddCommand(CMD_EXPAND, (s) => { try { Console.BufferWidth = 120; Console.WindowWidth = 120; Console.BufferHeight = 80; Console.WindowHeight = 40; } catch (Exception ex) { Term.Log(CMD_EXPAND + " error: " + ex.Message); } Console.Clear(); Term.ReDrawLog(); }, "Expadn console window."); // ====================================================================== // Term.AddCommand(CMD_MODULE, (s) => { moduleManager.EnterGUI(); }, "Enter modules manager."); // ====================================================================== // Term.AddCommand(CMD_COMMANDS, (s) => { Term.ShowAllCommands(); }, "Show all commands."); }