I posted a question earlier asking why does my server (written in C++ and boost::asio) can't connect with a client (written in Javascript). Is the problem that the Javascript Websockets are different than boost::asio sockets ? Does boost::asio not support websockets ? What is the easiest way to work this out ?
Asked
Active
Viewed 4,925 times
6
Community
- 1
- 1
dimitris93
- 3,977
- 10
- 46
- 82
-
1Your best bet is probably [websocket++](https://github.com/zaphoyd/websocketpp). It has a `boost::asio` transport. As it is, you need to write your own websocket protocol code on top of `asio`, or use an existing library like websocket++. – Sean Cline Apr 11 '16 at 23:49
-
@SeanCline Can I not make javascript work with sockets instead ? – dimitris93 Apr 11 '16 at 23:51
-
@Shiro See [this question](http://stackoverflow.com/q/1736382/3962537) about that. – Dan Mašek Apr 11 '16 at 23:54
-
@DanMašek Let me ask a different question. Is it lame to use websockets instead of sockets to communicate with a c++ application on a server and an android application in java ? – dimitris93 Apr 11 '16 at 23:56
-
@Shiro I'm not familiar with the exact criteria to evaluate lameness, but depending on circumstances it might not be the best design decision. I'm not an android developer, but since you mention Java, a quick search nets [this](http://developer.android.com/reference/java/net/Socket.html). However, let's say you wanted to have support for both java apps, as well as some web-based application. In that case building your protocol on top of websockets might be a good choice. – Dan Mašek Apr 12 '16 at 00:02
-
1@DanMašek Yes, the problem is that I want to have support on android as well as browser. This is why I am thinking how will I make this work. – dimitris93 Apr 12 '16 at 00:03
-
@Shiro In that case, I'd probably choose websockets, just so that there's only one server implementation to deal with. On Android, I'd probably look for some [Java implementation of websockets](https://github.com/TooTallNate/Java-WebSocket), so you don't need to do the communications in JS. – Dan Mašek Apr 12 '16 at 00:09
-
1@DanMašek For the record, `Wesocketpp` worked perfectly. Thank you. It is by far the easiest way to use websockets in C++. First install `boost` and then add `Websocketpp` as a header-only library to a project. – dimitris93 Apr 13 '16 at 12:02
1 Answers
8
Boost.Asio doesn't directly support WebSocket but there's this great open source library out there which is very closely modeled to Boost.Asio and works the way that you expect. You might give it a try, its header-only and uses just boost. It comes with example code and documentation: http://vinniefalco.github.io/
Here's a complete program that sends a message to the echo server:
#include <beast/websocket.hpp>
#include <beast/buffers_debug.hpp>
#include <boost/asio.hpp>
#include <iostream>
#include <string>
int main()
{
// Normal boost::asio setup
std::string const host = "echo.websocket.org";
boost::asio::io_service ios;
boost::asio::ip::tcp::resolver r(ios);
boost::asio::ip::tcp::socket sock(ios);
boost::asio::connect(sock,
r.resolve(boost::asio::ip::tcp::resolver::query{host, "80"}));
using namespace beast::websocket;
// WebSocket connect and send message using beast
stream<boost::asio::ip::tcp::socket&> ws(sock);
ws.handshake(host, "/");
ws.write(boost::asio::buffer("Hello, world!"));
// Receive WebSocket message, print and close using beast
beast::streambuf sb;
opcode op;
ws.read(op, sb);
ws.close(close_code::normal);
std::cout <<
beast::debug::buffers_to_string(sb.data()) << "\n";
}
Vinnie Falco
- 4,950
- 24
- 39