Jump to content
Sign in to follow this  
CodeTinkerer

Websocket Client help

Recommended Posts

CodeTinkerer

Hello,

Before I begin, let me mention I have indeed looked through various posts and examples of anything pertaining to WebSocket. One thread Thread had some motivation long ago but never took off. Another Thread ended in hits and lack of support.

 

Any insight would be welcomed and appreciated :)

 

In my current progress, I end up with a 400 bad request response, which is indicative that my message is not understood.

 

 

#include "WinHttp.au3"
#include <WinAPI.au3>;_WinAPI_GetLastError
#include "JSON.au3"
#include "JSON_Translate.au3"


Global Const $ERROR_NOT_ENOUGH_MEMORY = 8
Global Const $ERROR_INVALID_PARAMETER = 87

Global Const $WINHTTP_OPTION_UPGRADE_TO_WEB_SOCKET = 114

Global Const $WINHTTP_WEB_SOCKET_BINARY_MESSAGE_BUFFER_TYPE = 0
Global Const $WINHTTP_WEB_SOCKET_BINARY_FRAGMENT_BUFFER_TYPE = 1

Global Const $WINHTTP_WEB_SOCKET_SUCCESS_CLOSE_STATUS = 1000

Global $hOpen = 0, $hConnect = 0, $hRequest = 0, $hWebSocket = 0
Global $iError = 0

Example()
Exit

Func Example()
    Local $sServerName = "104.16.107.31"


    Local $sMessage = "Hello world"

    ; 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, "104.16.107.31", $INTERNET_DEFAULT_HTTP_PORT)
    If $hConnect = 0 Then
        $iError = _WinAPI_GetLastError()
        ConsoleWrite("Connect error" & @CRLF)
        Return False
    EndIf

    $hRequest = _WinHttpOpenRequest($hConnect, "POST", $sPath, "")
    If $hRequest = 0 Then
        $iError = _WinAPI_GetLastError()
        ConsoleWrite("OpenRequest error" & @CRLF)
        Return False
    EndIf
    ; Add header fields to the request
    _WinHttpAddRequestHeaders($hRequest, "Connection: Upgrade")
    _WinHttpAddRequestHeaders($hRequest, "Upgrade: websocket")
    ; 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("SendRequest error" & @CRLF)
        Return False
    EndIf
    ConsoleWrite("HTTPResp: " & $fStatus & @CRLF)

    TCPStartup()

    Local $iSocket = TCPConnect($sServerName, 80)
    If @error Then
        ConsoleWrite("Error in TCP Connection" & @CRLF)
    Else
        ConsoleWrite("Connected to Socket: " & $iSocket & @CRLF)
    EndIf

    Local $s = '{' _
             & '"type": "subscribe", ' _
             & '"channels": [{"name": "ticker", ' _
             & '"product_ids": ["ETH-EUR"]}]' _
             & '}'
    $s = String($s)
    Local $t = _JSONEncode(_JSONDecode($s))
    ConsoleWrite("RawJson: " & $s & @CRLF)
    ConsoleWrite("Encoded: " & $t & @CRLF)
    ;_CWrite("_JSONEncode:" & $s)

    TCPSend($iSocket, $t)
    If @error Then
        ConsoleWrite("Error in TCP Send " & @error & @CRLF)
    EndIf

    Local $irecv = TCPRecv($iSocket, 2048)
    If @error Then
        ConsoleWrite("Error in TCP Recv " & @error & @CRLF)
    Else
        ConsoleWrite("$irecv: >>> " & @CRLF & $irecv & @CRLF)
        Local $iListen = TCPListen($sServerName, 80)
        If @error Then
            ConsoleWrite("Error in TCP Listen " & @error & @CRLF)
        Else
            Do
                $iSocket = TCPAccept($iSocket)

                If @error Then
                    ConsoleWrite("Error in TCP Accept" & @CRLF)
                    ExitLoop
                Else
                    ConsoleWrite("Recv: " & $iSocket & @CRLF)
                EndIf
            Until $iSocket < 0
        EndIf
    EndIf
EndFunc   ;==>Example

 

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×