コード例 #1
0
        internal WebSocketContext RegisterNewWebSocket(
            Socket clientSocket,
            string initUrl,
            string sec_websocket_key,
            string sec_websocket_extensions)
        {
            PlainWebSocketConn wbcontext = new PlainWebSocketConn(false);
            var wbCtx = new WebSocketContext(wbcontext);

            wbCtx.InitClientRequestUrl = initUrl;
            AssignWebSocketExtensions(wbCtx, sec_websocket_extensions);

            _workingWebSocketConns.Add(wbcontext.ConnectionId, wbCtx);                                            //add to working socket
            wbcontext.InitClientRequestUrl = initUrl;
            wbcontext.Bind(clientSocket, MakeWebSocketUpgradeResponse(MakeResponseMagicCode(sec_websocket_key))); //move client socket to webSocketConn
            _newContextConnected?.Invoke(wbCtx);
            return(wbCtx);
        }
コード例 #2
0
            public void Connect(string url)
            {
                var plainWsConn = new PlainWebSocketConn(true);

                plainWsConn.Compression = this.Compression;
                plainWsConn.SetMessageHandler(_websocketHandler);
                _wbsocketConn = plainWsConn;

                Uri uri = new Uri(url);

                InitClientSocket(uri);

                _clientSocket.Connect(_hostIP, uri.Port);
                //create http webreq

                StringBuilder stbuilder = CreateWebSocketUpgradeReq(uri.AbsolutePath, uri.AbsolutePath + ":" + uri.Port, plainWsConn.Compression);

                byte[] dataToSend = Encoding.ASCII.GetBytes(stbuilder.ToString());
                //****
                //add event listener to our socket
                plainWsConn.Bind(_clientSocket, dataToSend);
            }