Comments (7)
I would maybe do something like this:
#include "server_ws.hpp"
#include <unordered_map>
using namespace std;
typedef SimpleWeb::SocketServer<SimpleWeb::WS> WsServer;
class Player {
};
int main() {
WsServer server;
server.config.port = 8080;
unordered_map<std::shared_ptr<WsServer::Connection>, Player> connections;
auto &echo = server.endpoint["^/echo/?$"];
echo.on_message = [&server](shared_ptr<WsServer::Connection> connection, shared_ptr<WsServer::Message> message) {
};
echo.on_open = [&connections](shared_ptr<WsServer::Connection> connection) {
connections.emplace(connection, Player());
};
//See RFC 6455 7.4.1. for status codes
echo.on_close = [&connections](shared_ptr<WsServer::Connection> connection, int status, const string & /*reason*/) {
connections.erase(connection);
};
//See http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference.html, Error Codes for error code meanings
echo.on_error = [&connections](shared_ptr<WsServer::Connection> connection, const boost::system::error_code &ec) {
connections.erase(connection);
};
server.start();
}
edit: decided to use shared_ptr in unordered_map<std::shared_ptr<WsServer::Connection>, Player> connections;
for simplicity
from simple-websocket-server.
Of course the above example takes it for granted that you run the server in one thread. If more threads are used, you would need a connections_mutex
that needs to be locked whenever emplace or erase is called.
from simple-websocket-server.
Thank you, that fast answer, i will try your solution.
And, out of curiosity, why don't you put any getter on the status ?
from simple-websocket-server.
If it is needed, I'll add the getter. You mean a getter for socket::is_open I take it?
from simple-websocket-server.
I was thinking something like the javascript implementation of WebSocket with a readyState that could be an enum
Constant | Value | Description |
---|---|---|
CONNECTING | 0 | The connection is not yet open. |
OPEN | 1 | The connection is open and ready to communicate. |
CLOSING | 2 | The connection is in the process of closing. |
CLOSED | 3 | The connection is closed or couldn't be opened. |
so we can have a:
socket::getReadyState;
from simple-websocket-server.
I do not immediately see a need for this. Also, one can easily implement this oneself using the on_open, on_close/on_error, on_message handlers.
from simple-websocket-server.
yes it's true but i think it will be nice if we can get the status of the socket somehow without having to redo the work twice.
if i have the time i'll try to do a pull request with a readyState and if you like it you can merge it.
from simple-websocket-server.
Related Issues (20)
- [suggest] Send/Broadcast and Text/BinaryMode HOT 1
- Standalone Asio not found - but asio.hpp is present HOT 4
- re again WssClient::start causes on_error HOT 10
- When I use Google Chrome it happend a error. HOT 3
- exception thrown in server start (boost related) HOT 4
- Connection Timeout HOT 2
- Create example on how to stream data to a client HOT 1
- Unable to compile against standalone asio HOT 1
- How to stop the server? HOT 3
- Installed but ws_examples throws an error HOT 1
- Support for the Sec-WebSocket-Protocol field HOT 3
- fatal error C1083: Cannot open include file : 'pthread.h': No such file or directory HOT 1
- Messages split among frames aren't reasembled HOT 25
- Openssl wants void* instead of char* in 0x1000115fL HOT 1
- how to initiate send message from client HOT 3
- Client request error: End of file
- Access violation exeption HOT 3
- gdiplus
- Can't use wss client HOT 5
- Integration with Simple-Web-Server HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from simple-websocket-server.