Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Websocket

cosmicsnake

Technical User
Oct 21, 2024
2
0
1
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
 
The connection must be made without headers otherwise it will cause bad data too!
I think that's were you error.

According to Chilkats websocket connection example there are mandatory headers to set:

* Add the standard WebSocket open handshake headers that will be needed.
* (This adds the required HTTP request headers to the rest object.)
loWs.AddClientHeaders()
I read it as not adding header to the Websocket object, but to the Rest object the Websocket object uses.

Not sure whether that is mandatory for websockets or just Chilkats class, I also haven't looked at other examples. It's the first thing that I find explaining your problem in a misconception.

Maybe Chilkat only supports Websockets based on http, whereas what you want to use is a websocket based client/server or even peer to peer communiccation not based on http. Therefore, you'd need to look for something else, maybe even base it on winsock.
 
Last edited:
Hi,
Thanks for the answer!
If i try loWs.AddClientHeaders()

Use this code:
Local loRest
Local lnSuccess
Local loWs
Local lcResponseBody
Local lnStatusCode


* For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.Rest')
loRest = Createobject('Chilkat_9_5_0.Rest')
lnSuccess = loRest.Connect("127.0.0.1",8092,0,0)
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

* Add the standard WebSocket open handshake headers that will be needed.
* (This adds the required HTTP request headers to the rest object.)
loWs.AddClientHeaders()

* Add any additional headers that might be desired.
* Two common WebSocketSpecific headers are "Sec-WebSocket-Protocol" and "Origin".
* loRest.AddHeader("Sec-WebSocket-Protocol","x-something")
* loRest.AddHeader("Origin","http://someserver.com")

* Do the open handshake.
lcResponseBody = loRest.FullRequestNoBody("GET","/something")
If (loRest.LastMethodSuccess <> 1) Then
msg( loRest.LastErrorText )
Release loRest
Release loWs
* CANCEL
Endif

lnStatusCode = loRest.ResponseStatusCode

msg ("Response status code: " + Str(lnStatusCode) )

If (lnStatusCode <> 101) Then
msg( lcResponseBody )
msg( "-- Failed because of unexpected response status code.")
Release loRest
Release loWs
*CANCEL
Endif


lnSuccess = loWs.ValidateServerHandshake()
IF (lnSuccess <> 1) THEN
msg( astr( loWs.LastErrorText ))
RELEASE loRest
RELEASE loWs
CANCEL
ENDIF

Give this error:

ChilkatLog:
FullRequestNoBody:
DllDate: Nov 17 2022
ChilkatVersion: 9.5.0.93
UnlockPrefix: PHCSFT.CB1112023
Architecture: Little Endian; 32-bit
Language: ActiveX
VerboseLogging: 0
uriPath: /something
fullRequestBody:
autoReconnect: 0
Sending request with no body...
sendReqNoBody:
sendReqHeader:
omitContentType: 0
sendReqHeader2:
checkEstablishConnection:
The connection already exists, as far as we know..
--checkEstablishConnection
Updated Host header to last connected host
Host: 127.0.0.1:8092
--sendReqHeader2
--sendReqHeader
--sendReqNoBody
Sent request.
fullRequestGetResponse:
Reading response header...
The server did not respond in 30000 milliseconds.
Try increasing the value of your Rest.IdleTimeoutMs property.
Failed to read response header.
--fullRequestGetResponse
--fullRequestBody
Failed.
--FullRequestNoBody
--ChilkatLog


The log of websocket:
"21/10/2024 17:15:54.410,; Winsock.Event(ConnectionRequest, IP=127.0.0.1)"
"21/10/2024 17:15:54.410,GET /something HTTP/1.1 Connection: Upgrade Host: 127.0.0.1:8092 Upgrade: websocket Sec-WebSocket-Key: D3sUQxPPxFIrzdQLBgnt4w== Sec-WebSocket-Version: 13 "
"21/10/2024 17:15:54.410,#ER:BAD_DATA#"
"21/10/2024 17:16:48.520,; Winsock.Event(Close)"
 
Well, you have to replace "something" in the sample with something you actually want to GET. You have to have a target URI.
 
Last edited:

Part and Inventory Search

Sponsor

Back
Top