Thank buddy for looking at it!
I managed to solve it finally!
The issue was what I already suspected and played with before, the
Sec-WebSocket-Extensions
Just leaving it out will make the client post messages without compression!
Here is the fixed code that works!
#include <Array.au3>
#include <Crypt.au3>
TCPStartup()
Global $gSocket = TCPListen("127.0.0.1", 65432)
While True
Local $client = TCPAccept($gSocket)
If $client <> -1 Then
Local $data = TCPRecv($client, 1024)
If StringInStr($data, "Sec-WebSocket-Key:") Then
; Perform WebSocket handshake
Local $key = StringRegExp($data, "Sec-WebSocket-Key: (.+)", 1)[0]
ConsoleWrite($data & @CRLF)
ConsoleWrite("$key:" & $key & @CRLF)
Local $responseKey = base64(_Crypt_HashData($key & "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", $CALG_SHA1))
Local $response = "HTTP/1.1 101 Switching Protocols" & @CRLF & _
"Upgrade: websocket" & @CRLF & _
"Connection: Upgrade" & @CRLF & _
"Sec-WebSocket-Accept: " & $responseKey & @CRLF & @CRLF
TCPSend($client, $response)
Sleep(1000)
WebSocketSend($client, "Hello!"); This works
Sleep(1000)
; Now the connection is upgraded to WebSocket, handle communication
While True
Local $message = WebSocketReceive($client)
If @error Then ContinueLoop
If $message <> "" Then
; Handle the received message
ConsoleWrite("Received message: " & $message & @CRLF)
EndIf
WEnd
EndIf
EndIf
WEnd
Func WebSocketReceive($socket)
Local $bytes = TCPRecv($socket, 4096) ; Adjust buffer size as needed
If @error Or StringLen($bytes) = 0 Then Return SetError(1, 0, 0) ; Error reading data
Return unmask(BinaryToString($bytes))
EndFunc
Func WebSocketSend($socket, $message)
Local $length = StringLen($message)
Local $header = Chr(BitOR(0x80, 0x01)) ; FIN, Opcode Text
If $length <= 125 Then
$header &= Chr($length)
ElseIf $length <= 65535 Then
$header &= Chr(126) & Chr(BitShift($length, 8)) & Chr(BitAND($length, 0xFF))
Else
$header &= Chr(127)
For $i = 7 To 0 Step -1
$header &= Chr(BitShift($length, 8 * $i))
Next
EndIf
TCPSend($socket, $header & $message)
EndFunc
;==============================================================================================================================
; Function: base64($vCode [, $bEncode = True [, $bUrl = False]])
;
; Description: Decode or Encode $vData using Microsoft.XMLDOM to Base64Binary or Base64Url.
; IMPORTANT! Encoded base64url is without @LF after 72 lines. Some websites may require this.
;
; Parameter(s): $vData - string or integer | Data to encode or decode.
; $bEncode - boolean | True - encode, False - decode.
; $bUrl - boolean | True - output is will decoded or encoded using base64url shema.
;
; Return Value(s): On Success - Returns output data
; On Failure - Returns 1 - Failed to create object.
;
; Author (s): (Ghads on Wordpress.com), Ascer
;===============================================================================================================================
Func base64($vCode, $bEncode = True, $bUrl = False)
Local $oDM = ObjCreate("Microsoft.XMLDOM")
If Not IsObj($oDM) Then Return SetError(1, 0, 1)
Local $oEL = $oDM.createElement("Tmp")
$oEL.DataType = "bin.base64"
If $bEncode then
$oEL.NodeTypedValue = Binary($vCode)
If Not $bUrl Then Return $oEL.Text
Return StringReplace(StringReplace(StringReplace($oEL.Text, "+", "-"),"/", "_"), @LF, "")
Else
If $bUrl Then $vCode = StringReplace(StringReplace($vCode, "-", "+"), "_", "/")
$oEL.Text = $vCode
Return $oEL.NodeTypedValue
EndIf
EndFunc ;==>base64
Func unmask($text)
Local $length = StringToBinary(StringMid($text, 2, 1))
$length = BitAND(Number($length), 127)
Local $masks, $data
If $length = 126 Then
$masks = StringMid($text, 5, 4)
$data = StringMid($text, 9)
ElseIf $length = 127 Then
$masks = StringMid($text, 11, 4)
$data = StringMid($text, 15)
Else
$masks = StringMid($text, 3, 4)
$data = StringMid($text, 7)
EndIf
Local $result = ""
For $i = 0 To StringLen($data) - 1
$result &= Chr(BitXOR(Asc(StringMid($data, $i + 1, 1)), Asc(StringMid($masks, Mod($i, 4) + 1, 1))))
Next
Return $result
EndFunc