Danp2 Posted September 4, 2021 Share Posted September 4, 2021 6 hours ago, Danp2 said: For some reason the existing example is no longer running correctly for me. Have you tried to use the UDF? The issue above was due to echo.websocket.org no longer existing. The other site in the example still works. @jugadorFor some unknown reason, I'm not able to initiate a websocket connection to that site with the UDF. I can connect just fine with Weasel. Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
jugador Posted September 5, 2021 Author Share Posted September 5, 2021 @Danp2 Yes echo.websocket.org has been taken down then how can you able to connect echo.websocket.org through Weasel. If I try to connect to ws://echo.websocket.org through chilkat it failed at the time of handshake ;~ So in your case you will get error at time of _WinHttpOpenRequest $hRequest = _WinHttpOpenRequest($hConnect, "GET", $aSites[$i][1], "") and if you try wss://echo.websocket.org it will not connect ;~ So you will get error at time of _WinHttpConnect $hConnect = _WinHttpConnect($hOpen, $aSites[$i][0], $INTERNET_DEFAULT_HTTP_PORT) and if you asking about Yahoo, yes no problem connecting to it using chilkat. Link to comment Share on other sites More sharing options...
Danp2 Posted September 5, 2021 Share Posted September 5, 2021 @jugadorSorry for being unclear. When I said "that site" I was actually referring to the yahoo site that is your target. Using Autoit and WinHttp, I can make a regular connection but I'm unable to "upgrade" the connection to a websocket. Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
jugador Posted September 5, 2021 Author Share Posted September 5, 2021 (edited) @Danp2 try changing $hConnect = _WinHttpConnect($hOpen, $aSites[$i][0], $INTERNET_DEFAULT_HTTP_PORT) to ;https://finance.yahoo.com ;wss://streamer.finance.yahoo.com $hConnect = _WinHttpConnect($hOpen, $aSites[$i][0], $INTERNET_DEFAULT_HTTPS_PORT) ;port 443 Local $aSites[1][3] = [['streamer.finance.yahoo.com', '', '{"subscribe":["AMZN"]}']] ;or Local $aSites[1][3] = [['streamer.finance.yahoo.com', '/', '{"subscribe":["AMZN"]}']] Edited September 5, 2021 by jugador Link to comment Share on other sites More sharing options...
Danp2 Posted September 5, 2021 Share Posted September 5, 2021 17 minutes ago, jugador said: $hConnect = _WinHttpConnect($hOpen, $aSites[$i][0], $INTERNET_DEFAULT_HTTPS_PORT) ;port 443 Already tried that. It gets further, but still fails to upgrade the connection to a websocket. The HTTP status code returned is 400, which indicates a bad request. Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
jugador Posted September 5, 2021 Author Share Posted September 5, 2021 (edited) @Danp2 check if AddHeader work ;_WinHttpAddRequestHeaders($hRequest, "Connection:Upgrade") ;_WinHttpAddRequestHeaders($hRequest, "Upgrade:websocket") _WinHttpAddRequestHeaders($hRequest, "Host:streamer.finance.yahoo.com") _WinHttpAddRequestHeaders($hRequest, "Origin:https://finance.yahoo.com") Edited September 5, 2021 by jugador Link to comment Share on other sites More sharing options...
Danp2 Posted September 5, 2021 Share Posted September 5, 2021 No change with added headers. Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
jugador Posted September 5, 2021 Author Share Posted September 5, 2021 @Danp2 ;~ Do the open handshake $o_Response = __ChilkatRest_FullRequestNoBody('GET', '/') ;~ Now if omit '/' I get 400 Bad Request $o_Response = __ChilkatRest_FullRequestNoBody('GET', '') so try this way Local $aSites[1][3] = [['streamer.finance.yahoo.com', '/', '{"subscribe":["AMZN"]}']] Link to comment Share on other sites More sharing options...
Danp2 Posted September 5, 2021 Share Posted September 5, 2021 @jugador That doesn't change anything for me. Here's the script that I'm using to test -- expandcollapse popup;http://code.msdn.microsoft.com/windowsdesktop/WinHTTP-WebSocket-sample-50a140b5/sourcecode?fileId=51199&pathId=1032775223 #include "WinHttp_WebSocket.au3" #include <WinAPI.au3> ;_WinAPI_GetLastError #include <APIErrorsConstants.au3> Global $hOpen = 0, $hConnect = 0, $hRequest = 0, $hWebSocket = 0 Global $iError = 0 Example() Exit quit() Func Example() ; Local $aSites[2][3] = [["echo.websocket.org", "", "Hello world"], ["ws.xtb.com", "demo", '{"command":"ping"}']] ; This service is no longer available ; Local $aSites[1][3] = [["echo.websocket.org", "", "Hello world"]] ; Local $aSites[2][4] = [["ws.xtb.com", $INTERNET_DEFAULT_HTTP_PORT, "demo", '{"command":"ping"}'], _ ; ["demo.piesocket.com", $INTERNET_DEFAULT_HTTP_PORT, "/v3/channel_1?api_key=oCdCMcMPQpbvNjUIzqtvF1d2X2okWpDQj4AwARJuAgtjhzKxVEjQU6IdCjwm¬ify_self", "testing"]] Local $aSites[1][4] = [['streamer.finance.yahoo.com', $INTERNET_DEFAULT_HTTPS_PORT, '/', '{"subscribe":["AMZN"]}']] For $i = 0 To UBound($aSites) - 1 ConsoleWrite("====================" & @crlf) ; Create session, connection and request handles. $hOpen = _WinHttpOpen("WebSocket sample", $WINHTTP_ACCESS_TYPE_DEFAULT_PROXY) If $hOpen = 0 Then $iError = _WinAPI_GetLastError() ConsoleWrite("Open error" & @CRLF) Return False EndIf $hConnect = _WinHttpConnect($hOpen, $aSites[$i][0], $aSites[$i][1]) If $hConnect = 0 Then $iError = _WinAPI_GetLastError() ConsoleWrite("Connect error" & @CRLF) Return False EndIf $hRequest = _WinHttpOpenRequest($hConnect, "GET", $aSites[$i][2]) If $hRequest = 0 Then $iError = _WinAPI_GetLastError() ConsoleWrite("OpenRequest error" & @CRLF) Return False EndIf ;~ _WinHttpAddRequestHeaders($hRequest, "Connection: Upgrade") ;~ _WinHttpAddRequestHeaders($hRequest, "Upgrade: websocket") ;~ _WinHttpAddRequestHeaders($hRequest, "Host:streamer.finance.yahoo.com") ;~ _WinHttpAddRequestHeaders($hRequest, "Origin:https://finance.yahoo.com") ; Request protocol upgrade from http to websocket. Local $fStatus = _WinHttpSetOptionNoParams($hRequest, $WINHTTP_OPTION_UPGRADE_TO_WEB_SOCKET) If Not $fStatus Then $iError = _WinAPI_GetLastError() ConsoleWrite("SetOption error" & @CRLF) Return False EndIf ; Perform websocket handshake by sending a request and receiving server's response. ; Application may specify additional headers if needed. $fStatus = _WinHttpSendRequest($hRequest) If Not $fStatus Then $iError = _WinAPI_GetLastError() ConsoleWrite("SendRequest error" & @CRLF) Return False EndIf $fStatus = _WinHttpReceiveResponse($hRequest) If Not $fStatus Then $iError = _WinAPI_GetLastError() ConsoleWrite("ReceiveResponse error" & @CRLF) Return False EndIf ; Application should check what is the HTTP status code returned by the server and behave accordingly. ; WinHttpWebSocketCompleteUpgrade will fail if the HTTP status code is different than 101. $iExtended = _WinHttpQueryHeaders($hRequest, $WINHTTP_QUERY_STATUS_CODE) If $iExtended <> $HTTP_STATUS_SWITCH_PROTOCOLS Then ;~ ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iExtended = ' & $iExtended & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console ConsoleWrite("Invalid status returned by _WinHttpQueryHeaders" & @CRLF) $iError = $iExtended Return False EndIf $hWebSocket = _WinHttpWebSocketCompleteUpgrade($hRequest, 0) ;~ ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $hWebSocket = ' & $hWebSocket & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console If $hWebSocket = 0 Then $iError = _WinAPI_GetLastError() ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iError = ' & $iError & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console ConsoleWrite("WebSocketCompleteUpgrade error" & @CRLF) Return False EndIf _WinHttpCloseHandle($hRequest) $hRequestHandle = 0 ConsoleWrite("Succesfully upgraded to websocket protocol" & @CRLF) ; Send and receive data on the websocket protocol. $iError = _WinHttpWebSocketSend($hWebSocket, _ $WINHTTP_WEB_SOCKET_UTF8_MESSAGE_BUFFER_TYPE, _ $aSites[$i][3]) If @error Or $iError <> 0 Then ConsoleWrite("WebSocketSend error" & @CRLF) Return False EndIf ConsoleWrite("Sent message to the server: " & $aSites[$i][3] & @CRLF) Local $iBufferLen = 1024 Local $tBuffer = 0, $bRecv = Binary("") Local $iBytesRead = 0, $iBufferType = 0 Do If $iBufferLen = 0 Then $iError = $ERROR_NOT_ENOUGH_MEMORY Return False EndIf $tBuffer = DllStructCreate("byte[" & $iBufferLen & "]") $iError = _WinHttpWebSocketReceive($hWebSocket, _ $tBuffer, _ $iBytesRead, _ $iBufferType) If @error Or $iError <> 0 Then ConsoleWrite("WebSocketReceive error" & @CRLF) Return False EndIf ; If we receive just part of the message restart the receive operation. $bRecv &= BinaryMid(DllStructGetData($tBuffer, 1), 1, $iBytesRead) $tBuffer = 0 $iBufferLen -= $iBytesRead Until $iBufferType <> $WINHTTP_WEB_SOCKET_UTF8_FRAGMENT_BUFFER_TYPE ; We expected server just to echo single binary message. If $iBufferType <> $WINHTTP_WEB_SOCKET_UTF8_MESSAGE_BUFFER_TYPE Then ConsoleWrite("Unexpected buffer type" & @CRLF) $iError = $ERROR_INVALID_PARAMETER Return False EndIf ConsoleWrite("Received message from the server: '" & BinaryToString($bRecv) & "'" & @CRLF) ; Gracefully close the connection. $iError = _WinHttpWebSocketClose($hWebSocket, _ $WINHTTP_WEB_SOCKET_SUCCESS_CLOSE_STATUS) If @error Or $iError <> 0 Then ConsoleWrite("WebSocketClose error" & @CRLF) Return False EndIf ; Check close status returned by the server. Local $iStatus = 0, $iReasonLengthConsumed = 0 Local $tCloseReasonBuffer = DllStructCreate("byte[123]") $iError = _WinHttpWebSocketQueryCloseStatus($hWebSocket, _ $iStatus, _ $iReasonLengthConsumed, _ $tCloseReasonBuffer) If @error Or $iError <> 0 Then ConsoleWrite("QueryCloseStatus error" & @CRLF) Return False EndIf ConsoleWrite("The server closed the connection with status code: '" & $iStatus & "' and reason: '" & _ BinaryToString(BinaryMid(DllStructGetData($tCloseReasonBuffer, 1), 1, $iReasonLengthConsumed)) & "'" & @CRLF) Next EndFunc ;==>Example Func quit() If $hRequest <> 0 Then _WinHttpCloseHandle($hRequest) $hRequest = 0 EndIf If $hWebSocket <> 0 Then _WinHttpCloseHandle($hWebSocket) $hWebSocket = 0 EndIf If $hConnect <> 0 Then _WinHttpCloseHandle($hConnect) $hConnect = 0 EndIf If $iError <> 0 Then ConsoleWrite("Application failed with error: " & $iError & @CRLF) Return -1 EndIf Return 0 EndFunc Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
jugador Posted September 5, 2021 Author Share Posted September 5, 2021 (edited) @Danp2 I am using windows 7 so can’t run your code. SetOption error Application failed with error: 12009 check this link, if any help https://www.example-code.com/vbscript/websocket.asp https://www.example-code.com/vbscript/websocket_tls.asp Edited September 5, 2021 by jugador Link to comment Share on other sites More sharing options...
Danp2 Posted September 5, 2021 Share Posted September 5, 2021 Unfortunately I don't see how those links would help in this instance. Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
Danp2 Posted September 5, 2021 Share Posted September 5, 2021 @jugador I was finally able to resolve the issue by passing an additional flag to _WinHttpOpenRequest when connecting to a secure site. 🎉😅 jugador 1 Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
jugador Posted September 6, 2021 Author Share Posted September 6, 2021 @Danp2 $WINHTTP_FLAG_SECURE Doing same thing using chilkat.... enabling TLS flag when connecting via port 443. Link to comment Share on other sites More sharing options...
jugador Posted September 9, 2021 Author Share Posted September 9, 2021 (edited) @Danp2 ProtobufToJson conversion working for me. Just that any suggestion from you or @Nine to make ProtobufToJson bit faster. @Danp2 have you look at blob object sample data. websocket do use blob or arraybuffer to send data. On google/forum search everything on Sql blob. for websocket blob only this.... https://developer.mozilla.org/en-US/docs/Web/API/Blob#extracting_data_from_a_blob Edited September 9, 2021 by jugador Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now