Пример #1
0
        public WebSocketServer(int port, string pfxPath, string password, bool useTask)
        {
            _useTask = useTask;
            var config = new ServerConfig
            {
                Ip               = "127.0.0.1",
                Port             = port,
                MaxRequestLength = 64 * 1024 * 1024,
                Security         = "tls, tls11, tls12",
                Certificate      = new CertificateConfig
                {
                    FilePath = pfxPath,
                    Password = password
                }
            };

            var _server = new SuperSocket.WebSocket.WebSocketServer();

            if (!_server.Setup(config, logFactory: new ConsoleLogFactory()))
            {
                throw new Exception("");
            }

            _server.NewSessionConnected += NewSessionConnected;
            _server.NewMessageReceived  += NewMessageReceived;
            _server.SessionClosed       += SessionClosed;
            _server.Start();
        }
Пример #2
0
        private void setup_server(ref WebSocketServer server, SuperSocket.SocketBase.Config.ServerConfig serverConfig)
        {
            var rootConfig = new SuperSocket.SocketBase.Config.RootConfig();

            server = new SuperSocket.WebSocket.WebSocketServer();

            //サーバーオブジェクト作成&初期化
            server.Setup(rootConfig, serverConfig);

            //イベントハンドラの設定
            //接続
            server.NewSessionConnected += HandleServerNewSessionConnected;
            //メッセージ受信
            server.NewMessageReceived += HandleServerNewMessageReceived;
            //切断
            server.SessionClosed += HandleServerSessionClosed;

            //サーバー起動
            server.Start();
        }
Пример #3
0
        static void Main(string[] args)
        {
            var appServer = new SuperSocket.WebSocket.WebSocketServer();

              //Setup the appServer
              if (!appServer.Setup(2012)) //Setup with listening port
              {
            Console.WriteLine("Failed to setup!");
            Console.ReadKey();
            return;
              }

              appServer.NewSessionConnected += new SessionHandler<WebSocketSession>(appServer_NewSessionConnected);
              appServer.SessionClosed += new SessionHandler<WebSocketSession, CloseReason>(appServer_SessionClosed);
              appServer.NewMessageReceived += new SessionHandler<WebSocketSession, string>(appServer_NewMessageReceived);
              Console.WriteLine();

              //Try to start the appServer
              if (!appServer.Start())
              {
            Console.WriteLine("Failed to start!");
            Console.ReadKey();
            return;
              }

              Console.WriteLine("The server started successfully, press key 'q' to stop it!");

              JsonObject MSG = new JsonObject();

              while (true)
              {
            MSG = new JsonObject();
            switch (Console.ReadKey().KeyChar)
            {
              case 'q':
            goto quit;
              case 'd':
            //Debug
            break;
              case 'g':
            MSG.Add("type", "change_cards");
            int[] resources = new int[] { 0, 0, 0, 0 };
            for (int i = 0; i < 4; i++)
            {
              Console.Write("r" + i.ToString() + ": ");
              int.TryParse(Console.ReadLine(), out resources[i]);
              MSG.Add("res" + (i + 1).ToString(), resources[i]);
            }
            break;
              case 't':
            MSG.Add("type", "text");
            Console.WriteLine();
            Console.Write("Message: ");
            MSG.Add("text", Console.ReadLine());
            break;
              case 'r':
            MSG.Add("type", "registered");
            int r1 = 0;
            string r2;

            Console.Write("PlayerID: ");
            int.TryParse(Console.ReadLine(), out r1);
            Console.Write("Playername: ");
            r2 = Console.ReadLine();

            MSG.Add("player_id", r1);
            MSG.Add("player_name", r2);
            MSG.Add("content_pack", "dev-pack");
            break;
              case 'n':
            MSG.Add("type", "next");
            int n1 = 0;

            Console.Write("PlayerID: ");
            int.TryParse(Console.ReadLine(), out n1);

            MSG.Add("player_id", n1);

               /* Dictionary<string, string> asdf = new Dictionary<string, string>();
            asdf.Add("key1", "val1");
            asdf.Add("key2", "val2");
            MSG.Add("DEBUG", asdf);*/
            break;
              case 'm':
            MSG.Add("type", "move");
            int m1 = 0;
            int m2 = 0;

            Console.Write("PlayerID: ");
            int.TryParse(Console.ReadLine(), out m1);
            Console.Write("LocationID: ");
            int.TryParse(Console.ReadLine(), out m2);

            MSG.Add("player_id", m1);
            MSG.Add("location_id", m2);
            break;
              case 'a':
            MSG.Add("type", "question");
            int a1 = 0;
            int a2 = 0;
            string a3 = "";
            string[] a4 = new string[0];

            Console.Write("PlayerID: ");
            int.TryParse(Console.ReadLine(), out a1);

            Console.Write("CharacterID: ");
            int.TryParse(Console.ReadLine(), out a2);

            Console.Write("Question: ");
            a3 = Console.ReadLine();

              addOption:
            Array.Resize(ref a4, a4.Length + 1);
            Console.Write("Option " + a4.Length.ToString() + ": ");
            a4[a4.Length - 1] = Console.ReadLine();
            if (Console.ReadKey().KeyChar == '+')
              goto addOption;

            MSG.Add("player_id", a1);
            MSG.Add("character_id", a2);
            MSG.Add("question", a3);
            MSG.Add("options", a4);
            break;
              //TODO: add chars for different commands
            }
            if (MSG.ContainsKey("type"))
            {
              foreach (WebSocketSession s in appServer.GetAllSessions())
              {
            s.Send(SimpleJson.SerializeObject(MSG));
              }
              Console.WriteLine("Message sent: " + SimpleJson.SerializeObject(MSG));
            }
            Console.WriteLine();
            continue;
              }
            quit:
              //Stop the appServer
              appServer.Stop();

              Console.WriteLine();
              Console.WriteLine("The server was stopped!");
              Console.ReadKey();
        }
Пример #4
0
        static void Main(string[] args)
        {
            var appServer = new SuperSocket.WebSocket.WebSocketServer();

            //Setup the appServer
            if (!appServer.Setup(2012)) //Setup with listening port
            {
                Console.WriteLine("Failed to setup!");
                Console.ReadKey();
                return;
            }

            appServer.NewSessionConnected += new SessionHandler <WebSocketSession>(appServer_NewSessionConnected);
            appServer.SessionClosed       += new SessionHandler <WebSocketSession, CloseReason>(appServer_SessionClosed);
            appServer.NewMessageReceived  += new SessionHandler <WebSocketSession, string>(appServer_NewMessageReceived);
            Console.WriteLine();

            //Try to start the appServer
            if (!appServer.Start())
            {
                Console.WriteLine("Failed to start!");
                Console.ReadKey();
                return;
            }

            Console.WriteLine("The server started successfully, press key 'q' to stop it!");

            JsonObject MSG = new JsonObject();

            while (true)
            {
                MSG = new JsonObject();
                switch (Console.ReadKey().KeyChar)
                {
                case 'q':
                    goto quit;

                case 'd':
                    //Debug
                    break;

                case 'g':
                    MSG.Add("type", "change_cards");
                    int[] resources = new int[] { 0, 0, 0, 0 };
                    for (int i = 0; i < 4; i++)
                    {
                        Console.Write("r" + i.ToString() + ": ");
                        int.TryParse(Console.ReadLine(), out resources[i]);
                        MSG.Add("res" + (i + 1).ToString(), resources[i]);
                    }
                    break;

                case 't':
                    MSG.Add("type", "text");
                    Console.WriteLine();
                    Console.Write("Message: ");
                    MSG.Add("text", Console.ReadLine());
                    break;

                case 'r':
                    MSG.Add("type", "registered");
                    int    r1 = 0;
                    string r2;

                    Console.Write("PlayerID: ");
                    int.TryParse(Console.ReadLine(), out r1);
                    Console.Write("Playername: ");
                    r2 = Console.ReadLine();

                    MSG.Add("player_id", r1);
                    MSG.Add("player_name", r2);
                    MSG.Add("content_pack", "dev-pack");
                    break;

                case 'n':
                    MSG.Add("type", "next");
                    int n1 = 0;

                    Console.Write("PlayerID: ");
                    int.TryParse(Console.ReadLine(), out n1);

                    MSG.Add("player_id", n1);

                    /* Dictionary<string, string> asdf = new Dictionary<string, string>();
                     * asdf.Add("key1", "val1");
                     * asdf.Add("key2", "val2");
                     * MSG.Add("DEBUG", asdf);*/
                    break;

                case 'm':
                    MSG.Add("type", "move");
                    int m1 = 0;
                    int m2 = 0;

                    Console.Write("PlayerID: ");
                    int.TryParse(Console.ReadLine(), out m1);
                    Console.Write("LocationID: ");
                    int.TryParse(Console.ReadLine(), out m2);

                    MSG.Add("player_id", m1);
                    MSG.Add("location_id", m2);
                    break;

                case 'a':
                    MSG.Add("type", "question");
                    int      a1 = 0;
                    int      a2 = 0;
                    string   a3 = "";
                    string[] a4 = new string[0];

                    Console.Write("PlayerID: ");
                    int.TryParse(Console.ReadLine(), out a1);

                    Console.Write("CharacterID: ");
                    int.TryParse(Console.ReadLine(), out a2);

                    Console.Write("Question: ");
                    a3 = Console.ReadLine();

addOption:
                    Array.Resize(ref a4, a4.Length + 1);
                    Console.Write("Option " + a4.Length.ToString() + ": ");
                    a4[a4.Length - 1] = Console.ReadLine();
                    if (Console.ReadKey().KeyChar == '+')
                    {
                        goto addOption;
                    }

                    MSG.Add("player_id", a1);
                    MSG.Add("character_id", a2);
                    MSG.Add("question", a3);
                    MSG.Add("options", a4);
                    break;
                    //TODO: add chars for different commands
                }
                if (MSG.ContainsKey("type"))
                {
                    foreach (WebSocketSession s in appServer.GetAllSessions())
                    {
                        s.Send(SimpleJson.SerializeObject(MSG));
                    }
                    Console.WriteLine("Message sent: " + SimpleJson.SerializeObject(MSG));
                }
                Console.WriteLine();
                continue;
            }
quit:
            //Stop the appServer
            appServer.Stop();

            Console.WriteLine();
            Console.WriteLine("The server was stopped!");
            Console.ReadKey();
        }