-
Notifications
You must be signed in to change notification settings - Fork 245
Add ChatWebsocket example #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jagernicolas
wants to merge
1
commit into
IronsDu:master
Choose a base branch
from
jagernicolas:example-ChatWebsocket
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| #include <iostream> | ||
| #include <string> | ||
| #include <condition_variable> | ||
| #include <mutex> | ||
| #include <list> | ||
| #include <regex> | ||
|
|
||
| #include <brynet/net/http/HttpService.hpp> | ||
| #include <brynet/net/http/HttpFormat.hpp> | ||
| #include <brynet/net/http/WebSocketFormat.hpp> | ||
| #include <brynet/net/wrapper/ServiceBuilder.hpp> | ||
| #include <brynet/net/wrapper/HttpServiceBuilder.hpp> | ||
| #include <brynet/base/AppStatus.hpp> | ||
|
|
||
| using namespace brynet; | ||
| using namespace brynet::net; | ||
| using namespace brynet::net::http; | ||
|
|
||
| std::string getBody() | ||
| { | ||
|
|
||
| return R""(<!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta content="text/html;charset=utf-8" http-equiv="Content-Type"> | ||
| <meta content="utf-8" http-equiv="encoding"> | ||
| </head> | ||
| <body> | ||
| <p>Message received: <span id="msgrec"></span></p> | ||
| <p>Message to send: <input id="mymsg"></span></p> | ||
| <button type="button" onclick="sendMessage()">Send Message</button> | ||
| </body> | ||
| <script> | ||
| var ws = new WebSocket("ws://localhost:_PORT_"); | ||
| var msgrec = document.getElementById("msgrec") | ||
| var mymsg = document.getElementById("mymsg") | ||
|
|
||
| ws.onmessage = function (evt) { | ||
| msgrec.innerHTML = evt.data; | ||
| }; | ||
|
|
||
| function sendMessage() { | ||
| if (ws) | ||
| ws.send(mymsg.value) | ||
| } | ||
| </script> | ||
| </html>)""; | ||
| } | ||
|
|
||
|
|
||
| int main(int argc, char **argv) | ||
| { | ||
| if (argc != 3) | ||
| { | ||
| fprintf(stderr, "Usage: <listen port> <net work thread num>\n"); | ||
| exit(-1); | ||
| } | ||
|
|
||
| std::mutex lockClients; | ||
| std::list<HttpSession::Ptr> clients; | ||
|
|
||
| const auto port = std::atoi(argv[1]); | ||
| auto service = TcpService::Create(); | ||
| service->startWorkerThread(atoi(argv[2])); | ||
|
|
||
| auto httpEnterCallback = [&port](const HTTPParser& httpParser, | ||
| const HttpSession::Ptr& session) { | ||
| (void)httpParser; | ||
| HttpResponse response; | ||
| auto body = std::regex_replace(getBody(), std::regex("_PORT_"), std::to_string(port)); | ||
|
|
||
| response.setBody(body); | ||
| std::string result = response.getResult(); | ||
| session->send(result.c_str(), result.size(), [session]() { | ||
| session->postShutdown(); | ||
| }); | ||
| }; | ||
|
|
||
| auto wsEnterCallback = [&clients, &lockClients](const HttpSession::Ptr& httpSession, | ||
| WebSocketFormat::WebSocketFrameType opcode, | ||
| const std::string& payload) { | ||
| std::cout << "frame enter of type:" << int(opcode) << std::endl; | ||
| std::cout << "payload is:" << payload << std::endl; | ||
|
|
||
| switch (opcode) { | ||
| case WebSocketFormat::WebSocketFrameType::CLOSE_FRAME: | ||
| clients.remove(httpSession); | ||
| break; | ||
| case WebSocketFormat::WebSocketFrameType::TEXT_FRAME: | ||
| // send back the message to each connected client | ||
| for (auto client : clients) { | ||
| lockClients.lock(); | ||
| auto frame = std::make_shared<std::string>(); | ||
| WebSocketFormat::wsFrameBuild(payload.c_str(), | ||
| payload.size(), | ||
| *frame, | ||
| WebSocketFormat::WebSocketFrameType::TEXT_FRAME, | ||
| true, | ||
| false); | ||
| client->send(frame); | ||
| lockClients.unlock(); | ||
| } | ||
| break; | ||
| case WebSocketFormat::WebSocketFrameType::ERROR_FRAME: | ||
| case WebSocketFormat::WebSocketFrameType::CONTINUATION_FRAME: | ||
| case WebSocketFormat::WebSocketFrameType::BINARY_FRAME: | ||
| case WebSocketFormat::WebSocketFrameType::PING_FRAME: | ||
| case WebSocketFormat::WebSocketFrameType::PONG_FRAME: | ||
| break; | ||
| } | ||
| }; | ||
|
|
||
| auto wsConnectedCallback = [&clients, &lockClients](const HttpSession::Ptr& httpSession, | ||
| const HTTPParser&) { | ||
| lockClients.lock(); | ||
| clients.push_back(httpSession); | ||
| lockClients.unlock(); | ||
| }; | ||
|
|
||
| auto closedCallback = [&clients, &lockClients](const HttpSession::Ptr& httpSession) { | ||
| clients.remove(httpSession); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe need lock mutex. |
||
| }; | ||
|
|
||
| wrapper::HttpListenerBuilder listenBuilder; | ||
| listenBuilder.configureService(service) | ||
| .configureSocketOptions({ | ||
| [](TcpSocket& socket) { | ||
| socket.setNodelay(); | ||
| }, | ||
| }) | ||
| .configureConnectionOptions({ | ||
| AddSocketOption::WithMaxRecvBufferSize(1024), | ||
| }) | ||
| .configureListen([port](wrapper::BuildListenConfig builder) { | ||
| builder.setAddr(false, "0.0.0.0", port); | ||
| }) | ||
| .configureEnterCallback([httpEnterCallback, wsEnterCallback, wsConnectedCallback, closedCallback] | ||
| (const HttpSession::Ptr& httpSession, HttpSessionHandlers& handlers) { | ||
| (void)httpSession; | ||
| handlers.setHttpCallback(httpEnterCallback); | ||
| handlers.setWSCallback(wsEnterCallback); | ||
| handlers.setWSConnected(wsConnectedCallback); | ||
| handlers.setClosedCallback(closedCallback); | ||
| }) | ||
| .asyncRun(); | ||
|
|
||
| while(true) | ||
| { | ||
| std::this_thread::sleep_for(std::chrono::seconds(1)); | ||
| if (brynet::base::app_kbhit()) | ||
| { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this lock is incorrectly