Home » U++ Library support » U++ Core » How to close the websocket connection
Re: How to close the websocket connection [message #49528 is a reply to message #49487] |
Sat, 24 February 2018 15:59   |
uppjj
Messages: 9 Registered: February 2018 Location: France
|
Promising Member |
|
|
thanks for your reply Mirek
In my case the server no longer called do () after launching close (), I should have better read the documentation!
The problem is rather client side, see below.
I encountered several difficulties to write a client application (IoT) with Websocket class for the real world of the web, but it does not miss much:
1) the connection header is currently fixed, it is essential to be able to modify it
=> it could be something like a Vector <String> to adapt before connecting.
2) Need a public function for really close the socket. Websoket :: Close does not close the socket but sends a request to the server.
If it does not respond for some reason, the client loops indefinitely (my first problem !)
I replaced the Close by Disconnect (), and added a new Close ():
void Disconnect (const String & msg = Null); // old Close (), just a message to the server
void Close () {socket-> Close ();} // real TCP close, even is server is sleeping...
3) Sending Masked requests is not available. This works:
add this function in class WebSocket (Inet.h) :
void SendTextMasked(const String& data) { SendRaw(MASK|FIN|TEXT, data); }
Change SendRaw() in WebSocket.cpp :
void WebSocket::SendRaw(int hdr, const String& data)
{
if(IsError())
return;
ASSERT(!close_sent);
LLOG("Send " << data.GetCount() << " bytes, hdr: " << Format("%04X",hdr));
// mask detect
int LocMask = (hdr & MASK)?0x80:0;
hdr &= 0xFF;
//---- header construct
// opcode
String header;
header.Cat(hdr);
// Length
int len = data.GetCount();
if(len > 65535) {
header.Cat(LocMask | 127);
header.Cat(0);
header.Cat(0);
header.Cat(0);
header.Cat(0);
header.Cat(byte(len >> 24));
header.Cat(byte(len >> 16));
header.Cat(byte(len >> );
header.Cat(byte(len));
}
else
if(len > 125) {
header.Cat(LocMask | 126);
header.Cat(byte(len >> );
header.Cat(byte(len));
}
else
header.Cat(LocMask |(int)len);
if (LocMask)
{
//add masking-key
byte Cle[4];
Cle[0] = Random();
Cle[1] = Random();
Cle[2] = Random();
Cle[3] = Random();
for(int i = 0; i < 4; i++) header.Cat(Cle[i]);
//---- send header with mask
Out(header);
//---- send masked data
if(data.GetCount() != 0)
{
char buf[32768];
int n = data.GetCount();
for(int i = 0; i < n; i++)
buf[i] = data[i] ^ (byte) Cle[i & 3];
Out(String(buf,n));
}
}
else
{
//---- send header (not masked)
Out(header);
//--- send data (not masked)
if(data.GetCount() != 0)
Out(data);
}
}
hope this can help
|
|
|
 |
|
How to close the websocket connection
By: Tess on Tue, 07 November 2017 11:37
|
 |
|
Re: How to close the websocket connection
|
 |
|
Re: How to close the websocket connection
By: mirek on Wed, 15 November 2017 08:57
|
 |
|
Re: How to close the websocket connection
|
 |
|
Re: How to close the websocket connection
By: uppjj on Wed, 14 February 2018 19:17
|
 |
|
Re: How to close the websocket connection
By: mirek on Sat, 17 February 2018 12:01
|
 |
|
Re: How to close the websocket connection
By: uppjj on Sat, 24 February 2018 15:59
|
 |
|
Re: How to close the websocket connection
By: mirek on Sat, 24 February 2018 17:07
|
 |
|
Re: How to close the websocket connection
By: uppjj on Wed, 28 February 2018 15:24
|
 |
|
Re: How to close the websocket connection
By: mirek on Wed, 28 February 2018 16:25
|
 |
|
Re: How to close the websocket connection
By: Klugier on Wed, 28 February 2018 23:27
|
 |
|
Re: How to close the websocket connection
By: uppjj on Thu, 01 March 2018 16:55
|
 |
|
Re: How to close the websocket connection
By: mirek on Fri, 02 March 2018 15:28
|
 |
|
Re: How to close the websocket connection
By: uppjj on Sat, 03 March 2018 00:18
|
 |
|
Re: How to close the websocket connection
By: mirek on Sat, 03 March 2018 10:19
|
 |
|
Re: How to close the websocket connection
By: uppjj on Sun, 04 March 2018 15:51
|
Goto Forum:
Current Time: Tue Aug 26 18:34:57 CEST 2025
Total time taken to generate the page: 0.06494 seconds
|