Jump to content

Using TSSLWsocket to receive websockets

Recommended Posts



This is perhaps not the best way to do it, but it sort of works for me, except that I am seems to receive some garbage characters and unwanted characters in the beginning of the received string.


Am i doing something wrong?





and other garbage characters



is there a bug in TSSLWsocket or is this by design in json structure?


I also have trouble receive very large json strings. it seem to be incomplete.. a workaround i have is to add lineend as #93#125  which is ]}



are there other updated websocket libraries using TWSocket or indy ?

i know there are one but its not maintenanced anymore


Share this post

Link to post

TSSLWsocket receives a stream of bytes, perhaps you are not correctly converting them into a string, might be UTF8, UTF16, ANSI or anything really.  No idea what websockets specifies.  Are you using a unicode or ANSI Delphi, the websockets project was ANSI only.


I'm not aware of any up to date websocket libraries for ICS. 



Share this post

Link to post

I have allways used for example:

ReceivedJSON := TSslWSocket(Sender).ReceiveStr;


for receiving my strings in any project i had earlier using normal TWSocket.. but ssl is new to me... 


if you have other suggestion on how to receive the whole string im all ears


Share this post

Link to post

For HTTP, the headers contain the content type and character set so it's easy to convert to a string, including entities like £, look at TSslHttpRest.TriggerRequestDone2 in OverbyteIcsSslHttpRest.pas:


// convert response to correct codepage, including entities
   if (Pos ('text/', FContentType) = 1) or  (Pos ('json', FContentType) <> 0) or  (Pos ('javascript', FContentType) <> 0) or  (Pos ('xml', FContentType) <> 0) then begin
         FResponseRaw := IcsHtmlToStr(FResponseStream, FContentType, true);



But I have no idea if websockets provides the content type, and you've not said if you are using Unicode.




Share this post

Link to post

BTW, the Json you receive from a websocket or by HTTP may not contain any line endings, so using ReceiveStr is a bad idea, it may not return complete lines, that might explain why your json was corrupted if there was no line ending at the end.  That is why the HTTP component receives content into a stream, and that stream is passed to IcsHtmlToStr to convert into something Delphi can handle.




Edited by Angus Robertson

Share this post

Link to post

ok, i will see if i can correct this.. i never really done much with streams before, but i will probably figure out sonthign


Share this post

Link to post

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