コード例 #1
0
        public void Run()
        {
            //websocket init
            Console.WriteLine("tetris server is starting... port:{0}", Port_WebSocket);
            var listener = new WebSocketServer();
            listener.NewMessageReceived += listener_NewMessageReceived;
            listener.NewSessionConnected += listener_NewSessionConnected;

            if (!listener.Setup(Port_WebSocket))
            {
                Console.WriteLine("failed to initialize.");
                return;
            }

            if (!listener.Start())
            {
                Console.WriteLine("failed to start.");
                return;
            }

            //transceiver init
            Console.WriteLine("transceiver is starting... port:{0}", Port_Transceiver);
            var tr = new Transceiver(null, 2013);

            isRunning = true;
            Thread th = new Thread(() =>
            {
                Packet p = new Packet();
                while (isRunning)
                {
                    p.Clear();
                    tr.Receive(ref p);

                    Console.WriteLine("received a packet: {0}", p.Peek());

                    if (conn != null)
                    {
                        try
                        {
                            conn.Send((string)p.Pop());
                        }
            #pragma warning disable 0168
                        catch (Exception ex)
                        {
                            //conn = null;
                        }
            #pragma warning restore 0168
                    }
                }
                tr.Dispose();
            });
            th.Start();

            Console.WriteLine("press enter to terminate server.");
            Console.ReadLine();

            isRunning = false;
            listener.Stop();
            Console.WriteLine("terminating server...");
        }
コード例 #2
0
        static void TestTransceiver()
        {
            Thread t = new Thread(() =>
            {
                using (var listener = new Transceiver(null, ServerPort))
                {
                    var p = new Packet();
                    listener.Receive(ref p);
                    Console.WriteLine("received: {0}",p.Pop());
                }
            });
            t.Start();

            using (var sender = new Transceiver(new IPEndPoint(GetLocalIP(), ServerPort), 0))
            {
                Thread.Sleep(500);
                var p = new Packet();
                p.Push("transceiver test");
                sender.Send(p);
            }

            Console.WriteLine("press enter to finish.");
            Console.ReadLine();
        }