boostorg / beast

HTTP and WebSocket built on Boost.Asio in C++11
http://www.boost.org/libs/beast
Boost Software License 1.0
4.32k stars 636 forks source link

websocket_server_sync example returns "bad method" error on handshake #1738

Closed SebMeunier closed 4 years ago

SebMeunier commented 4 years ago

Hi!

Version of Beast: 266

All relevant compiler information: Using Visual Studio 2017

Steps necessary to reproduce the problem

I just used the sample websocket_server_sync.cpp and tried to connect from https://www.websocket.org/echo.html

I ran the server code with 127.0.0.1 2974 as arguments from VS debugger

I tried to connect from https://www.websocket.org/echo.html with either:

I always get a "Bad method" system_error at the ws.accept();

I also tried with a C# client I have written that works with another WS server, I get this error log on the client side:

Error|WebSocket.doHandshake|Includes an invalid Sec-WebSocket-Protocol header.

Client code

I am not a C++ developer (anymore) but I need to create a simple websocket server that will send a message to connected client(s)

I might be missing something obvious in the server configuration, such as the "target name"? or "service name"? but some help would appreciated.

Thanks.

Seb

vinniefalco commented 4 years ago

Sounds like you are using a non-SSL server and trying to accept SSL clients. Try using this example instead: https://github.com/boostorg/beast/tree/develop/example/websocket/server/sync-ssl

SebMeunier commented 4 years ago

Hi @vinniefalco

Thanks for the quick answer. Removing the wss part in the JS client definitely helped to make the connection, as I don't really need SSL here.

Since I'm here, you might be able to give me some advice on the best approach for my use case:

As I embedded the server sync code in a class but the "session" is created in a thread, I'm not sure how I can keep a reference on the opened connections/sockets to be able to write my messages from my class.

Thanks for any help / suggestion you may have here.

Seb

vinniefalco commented 4 years ago

This example implements broadcasting: https://github.com/boostorg/beast/tree/develop/example/websocket/server/chat-multi

stale[bot] commented 4 years ago

This issue has been open for a while with no activity, has it been resolved?

SebMeunier commented 4 years ago

Sorry for the late news. Thanks again @vinniefalco for your help, it works like a charm with your last sample.