I just learned websockets but am still c++ ignorant.
I'm using websocket++ 0.3X, and it is a veritable godsend (can't wait for 1.0). If there are multiple concurrent connections, and one client sends the server a message, will the message trigger the handlers of all other clients? If not, how can this be done? (Is this multithreading?)
What I want to do is the obvious: update the database via a message from a client then update any other clients currently viewing the fields updated.
Sources:
http://www.zaphoyd.com/websocketpp/
https://github.com/zaphoyd/websocketpp/wiki
The on_message handler will be called only in the connection that received the message. That connection is responsible for updating the database and signaling to your program to send an update out to all other clients.
Take a look at the broadcast server example here: http://www.zaphoyd.com/websocketpp/manual/common-patterns/server-initiated-messages for a simple example of how to set this up.
Related
I'm looking into using the Boost::Beast websocket library to create an asynchronous bidirectional pipe to pass data between a server and a client. I leveraged some code from the async example (I can post some at a later time if necessary, don't have access to it now). I currently have a class which creates several threads running a SocketListener. When a client connects, it creates a Session shared_ptr to do the async read and write functions. The problem is, this session object will only write out when the client has sent me a message. I'm looking for an implementation that allows my server to write on demand to all the clients connected to it and also listen for incoming data from those connections.
Is this possible? Am I using the wrong technique for this? The other way I though this may be achievable is to have an incoming websocket and and outgoing websocket. Incoming would allow a client to drop configurations for the server and outgoing would just monitor a message queue and do a async write if a message is available.
Thanks!
Is this possible?
Yes
Am I using the wrong technique for this?
No
The other way I though this may be achievable is to have an incoming websocket and and outgoing websocket, and No respectively.
That is not necessary, a websocket stream is full-duplex. You can read and write at the same time.
outgoing would just monitor a message queue and do a async write if a message is available.
This is the correct approach, but you can do that in the same Session object that also handles the reads.
Here's an example that reads continuously and can also write full-duplex: https://github.com/vinniefalco/CppCon2018
I have a few question about socket in c++!
First question, let's say that he writes a server for the game in which he will play 200 people at once, but accept is blocked because he already serves one client, how to deal with it?
Second question, how to download a list of all currently connected clients, so that you can then send a message to everyone?
I have a few question about socket in c++!
For future reference, please post only one question at a time. If you have multiple questions, post them separately.
let's say that he writes a server for the game in which he will play 200 people at once, but accept is blocked because he already serves one client, how to deal with it?
Use sockets in non-blocking mode, using select()/(e)poll() or other callback mechanisms to know which sockets have pending activity and when.
Otherwise, use accept() in a separate thread than other thread(s) used to service connected clients.
how to download a list of all currently connected clients, so that you can then send a message to everyone?
The server is responsible for keeping track of its connected clients. Then it can loop through that list when needed.
If a client wants to send a message to every other client, the best option is for it to send a single message to the server and ask the server to relay the message to every other client.
Otherwise, the client would have to request the list from the server, and then send a message to every other client individually.
I have a server which uses boost.asio services. I use boost.log for logging with an asynchronous sink. The asio_io_service gets started within an own thread. The logger gets initialized in the main thread before starting the server. The server accepts connections with async_accept and reads and sends messages also asynchronously.
Now I have the problem, that the logger logs only messages when the server is doing something, e.g. receiving messages. When I log a message after logger initialization and before starting the server I can see this log message only after the first message was received by the server. But I want to log messages even when no server actions happen. Have you an idea what the problem is and how I can solve it?
Okay, the related stackoverflow-questions gave me the answer :) It was the by default disabled auto_flush. So this single line in the logger initialization was the solution:
sink->locked_backend()->auto_flush(true);
Hi to all the experts out there :)
This is my first question here.
Problem Description :
I have to write a Market Data Feed Handler. This is going to be a Windows Service, will be using two Sockets.
Socket A : For communication between Subscribing applications and Feed Handler (Feed Handler will be accepting the connection request and the Item Request).
Socket B : For communication between Feed Handler and External Market Data provider, like Reuters/Bloomberg.
In both the cases Request/Response will be using the same port.
Note : The volume of Data coming from the external system is low (External system will only send the information which has been subscribed for, at this point of time).
However later on we may want to scale it, some providers throw all the data, and Feed Handler has to filter out locally, based on the subscription.
My questions :
What threading model i should use?
Which I/O strategy i should use?
Keeping in mind both the cases, should i create separate Request/Response thread?
EDIT 1: After reading few tutorials on Winsock, i'm planning to use Event Objects for asynchronous behavior.
The point of concern here is that, a single thread should listen for incoming client connections (Accept them) and also Connect to other server, in turn send/recv on two different ports.
Thread A
1) Listening for incoming connections. (Continuous)
2) Receiving Subscribe/Unsubscribe request from connected clients. (Rarely)
3) Connect to the external server (Onetime only).
4) Forward the request coming from client to the external server. (Rarely)
5) Receive data from external server. (Continuous)
6) send this data back to the connected clients. (Continuous)
My question is can a single thread act as both Client and Server, using asynchronous I/O models?
Thanks in advance.
Deepak
The easiest threading model seems to be single threaded synchronous. If you need to implement a filter for a provider, implement that as a socket-in/socket-out separate process.
I have a client/server application that communicates via tcp/ip sockets over an often unreliable wireless network.
To make it responsive in case of a connection error,I created this protocol.
1) client sends a request
2) server confirms reception of request (1 second timeout)
3) server processes while client wait(may take up to 10 seconds) (20 seconds timeout)
4) server sends response
Sometimes the request command get lost (the client sends it over an open connection but the server never receives it), but with this protocol I know immediately if the command has been received and is going to be processed.
What I'm asking (I've made some test with RESTSharp and ServiceStack) is: is possible to do something like this with a webservice? where a client before the response receives a confirmation that the request has been received?
Thanks,
Mattia
It's strange to see this type of error handling in Application code since if a HTTP request wasn't successful it will already throw an Exception.
To get a confirmation receipt in Application code you would need to add a 2nd request implementing some kind of Push Event / Comet technique to get the server to notify the client of different events, e.g. what Request Ids have been received.