Esempio n. 1
0
        public static void ServerNewMessageRecevied(MyWebSocketSession session, string value)
        {
            Console.WriteLine("接收到新的文本消息:" + value);
            session.Send("当前SessionID:" + session.SessionID);

            session.MyCustomerId = value;
        }
Esempio n. 2
0
 private static void ServerSessionClosed(MyWebSocketSession session, CloseReason value)
 {
     Console.WriteLine("有断开的连接:" + value.ToString() + "     " + session.MyCustomerId);
 }
Esempio n. 3
0
        public static void ServerNewSessionConnected(MyWebSocketSession session)
        {
            Console.WriteLine(string.Format("-------新的连接{0}:", i++) + session.Host + session.Path);

            Console.WriteLine("Host:" + session.Host + ";");//服务器的ip
            Console.WriteLine("Uri:" + session.UriScheme + ";");
            Console.WriteLine("Path:" + session.Path + ";");
            Console.WriteLine("CurrentToken:" + session.CurrentToken + ";");
            Console.WriteLine("SessionID:" + session.SessionID + ";");
            Console.WriteLine("Connection:" + session.Connection + ";");
            Console.WriteLine("Origin:" + session.Origin + ";");
            Console.WriteLine("LocalEndPoint:" + session.LocalEndPoint + ";");
            Console.WriteLine("RemoteEndPoint:" + session.RemoteEndPoint);

            string name     = "";
            string password = "";

            //解析客户端传过来的参数
            var arr = session.Path.Split('?');

            if (arr.Length > 1)
            {
                string ps    = arr[1];
                var    array = ps.Split('&');
                foreach (string item in array)
                {
                    string key   = item.Split('=')[0];
                    string value = item.Split('=')[1];
                    if (key == "name")
                    {
                        name = value;
                    }
                    else if (key == "password")
                    {
                        password = value;
                    }
                }
            }

            //WebSocket 令牌机制
            //1. 服务器端为每个 WebSocket 客户端生成唯一的一次性 Token;
            //2. 客户端将 Token 作为 WebSocket 连接 URL 的参数(譬如 ws://echo.websocket.org/?token=randomOneTimeToken),发送到服务器端进行 WebSocket 握手连接;
            //3. 服务器端验证 Token 是否正确,一旦正确则将这个 Token 标示为废弃不再重用,同时确认 WebSocket 握手连接成功;如果 Token 验证失败或者身份认证失败,则返回 403 错误。


            //检查Origin头
            if (session.Origin != "aaaaa")
            {
            }

            //单IP可建立连接的最大连接数

            //验证身份
            if (name != "admin" || password != "123456")
            {
                //非法身份直接断开连接
                //session.Close((CloseReason)1);
                //session.Close(CloseReason.InternalError);
                //session.CloseWithHandshake(403, "非法身份");
            }
        }