I have a problem with my websocket. I recently switched my server from PHP to C ++ and used the POCO library, I used this particular script:
WebSocketServer.cpp
For one client side in C ++, I used snipet, which I found here in stackoverflow:
WebSocketClient.cpp
I changed the client a bit, first it sends a default message as soon as I connect to the server.
I wrapped all this with the Update function from my main application, that I constantly get messages:
void CLeagueStats :: Update( ) {
if(m_Connected) {
string msg = Recv( );
if(msg != "") {
}
} else if(GetTime() - LastReconnectTime > 90 || LastReconnectTime == 0) {
Connect( );
}
}
string CLeagueStats :: Recv( ) {
char receiveBuff[256];
int flags=0;
int rlen=m_psock->receiveFrame(receiveBuff,256,flags);
CONSOLE_Print("Recv: "+string(receiveBuff));
return string(receiveBuff);
}
Now I have written a completely simple javascript client that initializes the loading of a page:
<script type="text/javascript">
var ip_port = 'localhost:9980';
var ohc;
if (typeof (MozWebSocket) == 'function')
ohc = new MozWebSocket('ws://' + ip_port);
else
ohc = new WebSocket('ws://' + ip_port);
var self = this;
setTimeout(function() {
self.ohc.send("hey");
}, 500);
</script>
The server log contains both messages:
C ++ application: A WebSocket connection is established. Received frame (length = 5, flags = 0x81).
JavaScript: WebSocket. ( = 3, = 0x81).
++ , js-client:
[Websocket] Connecting to websocket.
[Websocket] Send: hello
[Websocket] Recv: hello
?
, ?
n = ws.receiveFrame(buffer, sizeof(buffer), flags);
app.logger().information(Poco::format("Frame received (length=%d, flags=0x%x).", n,unsigned(flags)));
ws.sendFrame(buffer, n, flags);
- ?