Skip to content
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

How can one bind the receive and send of the server to istream and ostream #979

Closed
omidb opened this issue Mar 28, 2021 · 3 comments
Closed

Comments

@omidb
Copy link

omidb commented Mar 28, 2021

Hi,

Thanks for this lib.
Is it possible to create an istream and ostream from the server and use them instead of set_message_handler and send? I saw some examples but they were not what I was looking for. I want to be able to get an i/o stream from the server and pass it through another function.

Thanks

@zaphoyd
Copy link
Owner

zaphoyd commented Mar 28, 2021

The WebSocket protocol is a message based protocol. WebSocket++ presents this protocol to the client application using the send method for outgoing and the message handler callback for incoming messages. This is the only interface.

WebSocket++ can read and write the encoded byte stream to a few different locations, including iostreams. Calling the send method in this case results in the framed and encoded messages being written to an ostream instead of a network socket. Similarly, if you have an encoded WebSocket protocol stream, WebSocket++ can read the raw bytes from an istream and will call the message handler once for every message decoded from the stream.

@omidb
Copy link
Author

omidb commented Mar 28, 2021

@zaphoyd Thanks. I've been using Scala for years now and stream abstractions there give you the opportunity to build streams of messages. So one can add messages to an outgoing stream or receive from an incoming one.

In this case, I was wondering if one can implement two stream-buffers that one keep pulling in messages as they come and one keep pushing them out as they come. This way one might be able to create one istream and one ostream. Then one can pipe a websocket to file or cout or cin.

@zaphoyd
Copy link
Owner

zaphoyd commented Jun 29, 2021

If you are talking about the entire WebSocket byte stream you can do that out of the box with the iostream transport. The bundled iostream_server example demonstrates an echo server that reads a WebSocket byte stream in on standard in and outputs a WebSocket byte stream on standard out.

This can be used to do things like play back a recorded WebSocket byte stream captured with wireshark, performance testing, automated test suites, etc.

You seem to be talking more about doing this at the application data level though. You can definitely write istream and/or ostream wrappers around WebSocket++'s message handler and send function. Making the interface between the async network I/O and the syncronous iostreams would be a bit awkward.

If you just want a program that reads data on standard in and pushes it out to the websocket and vice versa with standard out that doesn't need any special iostream wrappers. It is sufficient to have a message handler that writes to std::cout (or whatever stream you want) and another thread (or some other async/non-blocking stream read) that sits on standard in and calls websocketpp::send whenever it gets anything.

@omidb omidb closed this as completed Jul 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants