public void Connect(string url, System.Security.Cryptography.X509Certificates.X509Certificate2 cert) { var secureWsConn = new SecureWebSocketConn(true); secureWsConn.Compression = this.Compression; secureWsConn.SetMessageHandler(_websocketHandler); _wbsocketConn = secureWsConn; //TODO: review buffer management here*** byte[] buffer1 = new byte[2048]; byte[] buffer2 = new byte[2048]; IOBuffer recvBuffer = new IOBuffer(buffer1, 0, buffer1.Length); IOBuffer sendBuffer = new IOBuffer(buffer2, 0, buffer2.Length); var sockNetworkStream = new LowLevelNetworkStream(recvBuffer, sendBuffer); var secureStream = new SecureSockNetworkStream(sockNetworkStream, cert, delegate { return(true); }); //*** Uri uri = new Uri(url); InitClientSocket(uri); sockNetworkStream.Bind(_clientSocket); _clientSocket.Connect(_hostIP, uri.Port); bool _passConn = false; secureStream.AuthenAsClient(uri.Host, () => { //-------------- StringBuilder stbuilder = CreateWebSocketUpgradeReq(uri.AbsolutePath, uri.AbsolutePath + ":" + uri.Port, secureWsConn.Compression); byte[] dataToSend = Encoding.ASCII.GetBytes(stbuilder.ToString()); //int totalCount = dataToSend.Length; //int sendByteCount = _clientSocket.Send(dataToSend); secureWsConn.Bind(secureStream, dataToSend); _passConn = true; }); //------------ //wait System.Threading.Thread.SpinWait(10); while (!_passConn) { System.Threading.Thread.Sleep(2); } }
internal WebSocketContext RegisterNewWebSocket( SharpConnect.Internal2.AbstractAsyncNetworkStream clientNetworkStream, string initUrl, string sec_websocket_key, string sec_websocket_extensions) { var wbcontext = new SecureWebSocketConn(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(clientNetworkStream, MakeWebSocketUpgradeResponse(MakeResponseMagicCode(sec_websocket_key))); //move client socket to webSocketConn _newContextConnected?.Invoke(wbCtx); return(wbCtx); }