IXWebSocket/README.md

124 lines
4.1 KiB
Markdown
Raw Normal View History

2018-10-08 00:47:38 +02:00
## API
2018-09-27 23:56:48 +02:00
### Sending messages
2018-09-28 00:34:18 +02:00
`websocket.send("foo")` will send a message.
2018-09-27 23:56:48 +02:00
If the connection was closed and sending failed, the return value will be set to false.
### ReadyState
`getReadyState()` returns the state of the connection. There are 4 possible states.
1. ReadyState::Connecting - The connection is not yet open.
2. ReadyState::Open - The connection is open and ready to communicate.
3. ReadyState::Closing - The connection is in the process of closing.
4. ReadyState::Closed - The connection is closed or could not be opened.
2018-09-27 23:56:48 +02:00
### Open and Close notifications
The onMessage event will be fired when the connection is opened or closed. This is similar to the [Javascript browser API](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket), which has `open` and `close` events notification that can be registered with the browser `addEventListener`.
```
2019-07-23 23:04:45 +02:00
webSocket.setOnMessageCallback([](const ix::WebSocketMessagePtr& msg)
2018-09-27 23:56:48 +02:00
{
2019-06-09 21:02:02 +02:00
if (msg->type == ix::WebSocketMessageType::Open)
2018-09-27 23:56:48 +02:00
{
2018-10-27 20:46:11 +02:00
std::cout << "send greetings" << std::endl;
// Headers can be inspected (pairs of string/string)
std::cout << "Handshake Headers:" << std::endl;
2019-06-09 21:02:02 +02:00
for (auto it : msg->headers)
{
std::cout << it.first << ": " << it.second << std::endl;
}
2018-09-27 23:56:48 +02:00
}
2019-06-09 21:02:02 +02:00
else if (msg->type == ix::WebSocketMessageType::Close)
2018-09-27 23:56:48 +02:00
{
2018-10-27 20:46:11 +02:00
std::cout << "disconnected" << std::endl;
// The server can send an explicit code and reason for closing.
// This data can be accessed through the closeInfo object.
2019-06-09 21:02:02 +02:00
std::cout << msg->closeInfo.code << std::endl;
std::cout << msg->closeInfo.reason << std::endl;
2018-09-27 23:56:48 +02:00
}
}
);
```
### Error notification
A message will be fired when there is an error with the connection. The message type will be `ix::WebSocketMessageType::Error`. Multiple fields will be available on the event to describe the error.
2018-09-27 23:56:48 +02:00
```
2019-07-23 23:04:45 +02:00
webSocket.setOnMessageCallback([](const ix::WebSocketMessagePtr& msg)
2018-09-27 23:56:48 +02:00
{
2019-06-09 21:02:02 +02:00
if (msg->type == ix::WebSocketMessageType::Error)
2018-09-27 23:56:48 +02:00
{
std::stringstream ss;
2019-06-09 21:02:02 +02:00
ss << "Error: " << msg->errorInfo.reason << std::endl;
ss << "#retries: " << msg->eventInfo.retries << std::endl;
ss << "Wait time(ms): " << msg->eventInfo.wait_time << std::endl;
ss << "HTTP Status: " << msg->eventInfo.http_status << std::endl;
std::cout << ss.str() << std::endl;
2018-09-27 23:56:48 +02:00
}
}
);
```
### start, stop
1. `websocket.start()` connect to the remote server and starts the message receiving background thread.
2. `websocket.stop()` disconnect from the remote server and closes the background thread.
### Configuring the remote url
The url can be set and queried after a websocket object has been created. You will have to call `stop` and `start` if you want to disconnect and connect to that new url.
```
2018-09-28 00:43:31 +02:00
std::string url("wss://example.com");
2018-09-27 23:56:48 +02:00
websocket.configure(url);
```
2018-10-26 00:14:31 +02:00
### Ping/Pong support
Ping/pong messages are used to implement keep-alive. 2 message types exists to identify ping and pong messages. Note that when a ping message is received, a pong is instantly send back as requested by the WebSocket spec.
```
2019-07-23 23:04:45 +02:00
webSocket.setOnMessageCallback([](const ix::WebSocketMessagePtr& msg)
2018-10-26 00:14:31 +02:00
{
2019-06-09 21:02:02 +02:00
if (msg->type == ix::WebSocketMessageType::Ping ||
msg->type == ix::WebSocketMessageType::Pong)
2018-10-26 00:14:31 +02:00
{
2019-06-09 21:02:02 +02:00
std::cout << "pong data: " << msg->str << std::endl;
2018-10-26 00:14:31 +02:00
}
}
);
```
A ping message can be sent to the server, with an optional data string.
```
websocket.ping("ping data, optional (empty string is ok): limited to 125 bytes long");
2019-02-06 08:04:45 +01:00
```
2019-01-26 01:11:39 +01:00
### Heartbeat.
You can configure an optional heart beat / keep-alive, sent every 45 seconds
2019-03-18 00:08:28 +01:00
when there is no any traffic to make sure that load balancers do not kill an
2019-01-26 01:11:39 +01:00
idle connection.
```
webSocket.setHeartBeatPeriod(45);
2018-10-26 00:14:31 +02:00
```
### Supply extra HTTP headers.
You can set extra HTTP headers to be sent during the WebSocket handshake.
```
WebSocketHttpHeaders headers;
headers["foo"] = "bar";
webSocket.setExtraHeaders(headers);
```