cosmicsnake
Technical User
- Oct 21, 2024
- 2
Hi,
I need Help!
I'm trying to develop a connecting with websockt to send and receive messages with a websocket.
But I have some problems!
I'm using the dll of chillkat but I can try other approaches!
I can establish the connection, but when sending the message that has to be sent in a byte array, the websocket log records it as bad_data!
"21/10/2024 11:52:32.250,; Winsock.Event(ConnectionRequest, IP=127.0.0.1)"
"21/10/2024 11:52:32.250, .N vN . .N "
"21/10/2024 11:52:32.250,#ER:BAD_DATA#"
"21/10/2024 11:52:47.230,; Winsock.Event(Close)"
The connection must be made without headers otherwise it will cause bad data too!
"21/10/2024 11:33:59.740,GET / HTTP/1.1 Sec-WebSocket-Version: 13 Sec-WebSocket-Key: EZsSVAvw7cNU49+Prye4PQ== Connection: Upgrade Upgrade: websocket Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits Host: 127.0.0.1:8092 "
"21/10/2024 11:33:59.740,#ER:BAD_DATA#"
I send the code I'm using so they can help me more easily!
Thank you in advance for your help!
This is the code to start the connection:
Local loSocket, lcMessage, lnSuccess, lcHost, lnPort, laAddressList
* configs : host port
lcHost = "192.168.100.126"
lnPort = 8092
* For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.Rest')
loRest = Createobject('Chilkat_9_5_0.Rest')
**("127.0.0.1", 8092, .F., 5000)
lnSuccess = loRest.Connect("127.0.0.1",8092,.F.,2)
If (lnSuccess <> 1) Then
? loRest.LastErrorText
Release loRest
Cancel
Endif
* For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.WebSocket')
loWs = Createobject('Chilkat_9_5_0.WebSocket')
* Tell the WebSocket to use this connection.
lnSuccess = loWs.UseConnection(loRest)
If (lnSuccess <> 1) Then
? loWs.LastErrorText
Release loRest
Release loWs
Cancel
Endif
lcMessage = "#I#"
cHexResultado = StringToHex(lcMessage)
STRHEX = cHexResultado
Dimension ARYBYTES(Int(Len(STRHEX)/2))
HEXTOBYTE(STRHEX, @ARYBYTES)
loBinData = Createobject("Chilkat_9_5_0.BinData")
For I = 1 To Alen(ARYBYTES)
**loBinData.AppendByte(ARYBYTES)
loBinData.AppendByte(astr(ARYBYTES))
Next
* Sent
lnFinalFrame = 1
BinDatalnSuccess = loWs.SendFrameBd(loBinData, lnFinalFrame)
If lnSuccess <> 1 Then
msg( "Error: " + loWs.LastErrorText)
Return
Else
msg("Bytes sent.")
Endif
* Close
loWs.CloseConnection()
* release Socket
Release loWs
Release loBinData
* Convert string to hexadecimal
Function StringToHex(cString)
Local I, cHexString, cChar
cHexString = ""
For I = 1 To Len(cString)
cChar = Substr(cString, I, 1)
cHexString = cHexString + Transform(Asc(cChar), "@0")
Next
Return cHexString
Endfunc
* Function Hex to Byte
Function HEXTOBYTE
Parameter HEXTEXT, BYTES
Private HEXTEXT
Private X, Y
Private NIBBLE
For X = 1 To Alen(BYTES)
BYTES[X] = 0
Next
HEXTEXT = Upper(HEXTEXT)
For X = 1 To Len(HEXTEXT) Step 2
For Y = 0 To 1
NIBBLE = Asc(Substr(HEXTEXT, X + Y, 1))
If NIBBLE >= 65 .And. NIBBLE <= 70 && Letras A-F
NIBBLE = NIBBLE - 55
Else
If NIBBLE >= 48 .And. NIBBLE <= 57 && Números 0-9
NIBBLE = NIBBLE - 48
Endif
Endif
BYTES[INT(X / 2) + 1] = BYTES[INT(X / 2) + 1] + NIBBLE * (16 ^ (1 - Y))
Next
Next
Endfunc
I need Help!
I'm trying to develop a connecting with websockt to send and receive messages with a websocket.
But I have some problems!
I'm using the dll of chillkat but I can try other approaches!
I can establish the connection, but when sending the message that has to be sent in a byte array, the websocket log records it as bad_data!
"21/10/2024 11:52:32.250,; Winsock.Event(ConnectionRequest, IP=127.0.0.1)"
"21/10/2024 11:52:32.250, .N vN . .N "
"21/10/2024 11:52:32.250,#ER:BAD_DATA#"
"21/10/2024 11:52:47.230,; Winsock.Event(Close)"
The connection must be made without headers otherwise it will cause bad data too!
"21/10/2024 11:33:59.740,GET / HTTP/1.1 Sec-WebSocket-Version: 13 Sec-WebSocket-Key: EZsSVAvw7cNU49+Prye4PQ== Connection: Upgrade Upgrade: websocket Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits Host: 127.0.0.1:8092 "
"21/10/2024 11:33:59.740,#ER:BAD_DATA#"
I send the code I'm using so they can help me more easily!
Thank you in advance for your help!
This is the code to start the connection:
Local loSocket, lcMessage, lnSuccess, lcHost, lnPort, laAddressList
* configs : host port
lcHost = "192.168.100.126"
lnPort = 8092
* For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.Rest')
loRest = Createobject('Chilkat_9_5_0.Rest')
**("127.0.0.1", 8092, .F., 5000)
lnSuccess = loRest.Connect("127.0.0.1",8092,.F.,2)
If (lnSuccess <> 1) Then
? loRest.LastErrorText
Release loRest
Cancel
Endif
* For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.WebSocket')
loWs = Createobject('Chilkat_9_5_0.WebSocket')
* Tell the WebSocket to use this connection.
lnSuccess = loWs.UseConnection(loRest)
If (lnSuccess <> 1) Then
? loWs.LastErrorText
Release loRest
Release loWs
Cancel
Endif
lcMessage = "#I#"
cHexResultado = StringToHex(lcMessage)
STRHEX = cHexResultado
Dimension ARYBYTES(Int(Len(STRHEX)/2))
HEXTOBYTE(STRHEX, @ARYBYTES)
loBinData = Createobject("Chilkat_9_5_0.BinData")
For I = 1 To Alen(ARYBYTES)
**loBinData.AppendByte(ARYBYTES)
loBinData.AppendByte(astr(ARYBYTES))
Next
* Sent
lnFinalFrame = 1
BinDatalnSuccess = loWs.SendFrameBd(loBinData, lnFinalFrame)
If lnSuccess <> 1 Then
msg( "Error: " + loWs.LastErrorText)
Return
Else
msg("Bytes sent.")
Endif
* Close
loWs.CloseConnection()
* release Socket
Release loWs
Release loBinData
* Convert string to hexadecimal
Function StringToHex(cString)
Local I, cHexString, cChar
cHexString = ""
For I = 1 To Len(cString)
cChar = Substr(cString, I, 1)
cHexString = cHexString + Transform(Asc(cChar), "@0")
Next
Return cHexString
Endfunc
* Function Hex to Byte
Function HEXTOBYTE
Parameter HEXTEXT, BYTES
Private HEXTEXT
Private X, Y
Private NIBBLE
For X = 1 To Alen(BYTES)
BYTES[X] = 0
Next
HEXTEXT = Upper(HEXTEXT)
For X = 1 To Len(HEXTEXT) Step 2
For Y = 0 To 1
NIBBLE = Asc(Substr(HEXTEXT, X + Y, 1))
If NIBBLE >= 65 .And. NIBBLE <= 70 && Letras A-F
NIBBLE = NIBBLE - 55
Else
If NIBBLE >= 48 .And. NIBBLE <= 57 && Números 0-9
NIBBLE = NIBBLE - 48
Endif
Endif
BYTES[INT(X / 2) + 1] = BYTES[INT(X / 2) + 1] + NIBBLE * (16 ^ (1 - Y))
Next
Next
Endfunc