Suggestion on how to handle/answer multiple messages on the same data packet on stream

조회수 16회
읽지 않은 첫 메시지로 건너뛰기

hunter...@gmail.com

읽지 않음,
2015. 12. 29. 오전 11:12:5115. 12. 29.
받는사람 Ratchet
Hi to all,
I'm using the react stream to connect a client to a server (IoServer) on a local socket on the same machine.
In this way I can put the client on another machine on the same network (or even on complete separate network over the internet).
So I can have a network distributed application. Anyway initially it will be all on the same machine.
My issue now is that sometimes the server is sending multiple json messages on the same packet (events happen very quickly). I split the json packet using a specific string on the socket.
So i can split all the messages received on the single packet and put it on my own buffer ready to be processed.
The issue is that if i process all the messages in the buffer in a single call (each of this message provide an answer to be written back to the server) I call the write function on the stream each time and the write function do not flush the buffer of data to the server on each answer but try to accumulate it on its own buffer. When this buffer reach the limit (2048 bytes now) i got an exception.
Now I solved this issue with a 0.1 periodic timer that handle the messages from the server and send back the answer one by one but i was wandering if there is a more elegant way to do it using as much as possible the event driven architecture of the loop. Now the client process keeps an unuseful amount of cpu with the 0.1 periodic timer when for much of the time it have nothing to do do at all because there are no events on the socket.
For example something like to force the stream to flush the buffer after any write operation or someghing like this.
Hope someone have  a suggestion on a better way to do it.
Thank you in advance.
Bye
Marcello

전체답장
작성자에게 답글
전달
새 메시지 0개