emscripten-core / emscripten-core/emscripten

Can't connect to a websocket from Chrome.

Open
#19,100 11 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

**Version of emscripten/emsdk:**
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.34 (57b21b8fdcbe3ebb523178b79465254668eab408)
clang version 17.0.0 (https://github.com/llvm/llvm-project a031f72187ce495b9faa4ccf99b1e901a3872f4b)
Target: wasm32-unknown-emscripten
Thread model: posix

My emscripten app connects to a websocket in a C++ server. This server is based on boost beast, I've got their simple example:
```
void do_session(tcp::socket& socket)
{
try
{
// Construct the stream by moving in the socket
websocket::stream ws{std::move(socket)};

ws.set_option(websocket::stream_base::timeout::suggested(beast::role_type::server));
// Set a decorator to change the Server of the handshake
ws.set_option(websocket::stream_base::decorator(
[](websocket::response_type& res)
{
res.set(http::field::server, std::string(BOOST_BEAST_VERSION_STRING) + " websocket-server-sync");
}));

// Accept the websocket handshake
ws.accept();
//ws.accept(buffer.data());

for (;;)
{
// This buffer will hold the incoming message
beast::flat_buffer buffer;

// Read a message
ws.read(buffer);
}
}
catch (beast::system_error const& se)
{
// This indicates that the session was closed
if (se.code() != websocket::error::closed)
std::cerr << "Error: " << se.code().message() << std::endl;
}
catch (std::exception const& e)
{
std::cerr << "Error: " << e.what() << std::endl;
}
}

int main(int argc, char *argv[])
{
net::io_context ioc{1};
tcp::resolver resolver(ioc);
//tcp::acceptor acceptor{ioc, resolver.resolve("0.0.0.0", "8010")};
tcp::acceptor acceptor{ioc, {net::ip::make_address("0.0.0.0"), 8010}};

while (!service_context.exit)
{
tcp::socket socket{ioc};
acceptor.accept(socket);
logger->Info("Connection accepted");

std::thread{std::bind(&do_session, std::move(socket))}.detach();
}
}
```

In my emscripten app I use boost::asio sockets. Although, I far as I unserstand, it doesn't matter.
This is the params of the client app:
```
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_SDL=2")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_SDL_TTF=2")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_SDL_IMAGE=2")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_FREETYPE=1")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=4")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --preload-file ./fonts")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexperimental-library")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s ALLOW_MEMORY_GROWTH")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s NO_DISABLE_EXCEPTION_CATCHING")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s WEBSOCKET_SUBPROTOCOL='text'")
```

So, it works nice in Firefox (connects well). Also, it connects using a test extension in Chrome. But it doesn't work when I run the client in Chrome. In Debug I see:
```
WebSocket connection to 'ws://127.0.0.1:8010/' failed:
```
Stack trace points on createPeer(),
```
ws = new WebSocketConstructor(url, opts);
```
```
yutovo_web.worker.js:192 worker.js onmessage() captured an uncaught exception: boost::wrapexcept: write: Socket not connected [system:53]
```
Server exception: "End of file".
Any suggestions?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.