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

Need Example WebSocket ping #2802

Open
shakor0 opened this issue Jun 4, 2024 · 6 comments
Open

Need Example WebSocket ping #2802

shakor0 opened this issue Jun 4, 2024 · 6 comments

Comments

@shakor0
Copy link

shakor0 commented Jun 4, 2024

Hello,

Some websockets are not compliant and we need to be able to sendPings once in a while manually.
Please show an example on how to do this for a connectWebSocket (Client) stream.

Thank you.

@s-ludwig
Copy link
Member

s-ludwig commented Jun 4, 2024

A simple ping can be sent like this:

ws.send((msg) { msg.write([]); }, FrameOpcode.ping);

This will send a ping message and should trigger a matching pong on the remote side. However, it will not count as a regular ping/pong within vibe.d's logic, so all it does is generate some network activity.

Do you know what exactly goes wrong on the connection? Are no pings generated at all by the server?

@shakor0
Copy link
Author

shakor0 commented Jun 4, 2024

It looks like some websocket servers expect the client to start pinging before sending an pong back, if the client does not send a ping the connection will get dropped.

What is msg in your example?

@s-ludwig
Copy link
Member

s-ludwig commented Jun 4, 2024

Okay, sounds like this should work then, no idea whether the server expects any actual payload in the ping message. Maybe it also makes sense to add a new field HTTPClientSettings.webSocketPingInterval that would optionally enable the same ping logic as the WebSocket server currently performs.

msg is an OutgoingWebSocketMessage instance.

@shakor0
Copy link
Author

shakor0 commented Jun 4, 2024

Adding HTTPClientSettings.webSocketPingInterval for the client would indeed solve these issues.

@MrX273
Copy link

MrX273 commented Jun 6, 2024

Does not work for me, could you show me a working example?


import vibe.core.core;
import vibe.core.log;
import vibe.http.fileserver : serveStaticFiles;
import vibe.http.router : URLRouter;
import vibe.http.server;
import vibe.web.web;
import vibe.http.websockets;
import vibe.vibe;


int main(string[] args)
{
	string WS_URL= "wss://echo.websocket.org";
	auto ws_url = URL(WS_URL);
	const use_tls = (ws_url.schema == "wss" || ws_url.schema == "https") ? true : false;
    ws_url.schema = use_tls ? "https" : "http";
    auto receiver = connectWebSocket(ws_url);
	
	
	ConnectionStream m_conn;
	RandomNumberStream m_rng;
	
	auto message = new OutgoingWebSocketMessage(m_conn, FrameOpcode.ping, m_rng);

	while (receiver.waitForData())
	 {
			auto msg = parseJsonString(receiver.receiveText());
			
			try {
					receiver.send((message) { message.write([]); }, FrameOpcode.ping);
				}
				catch (Exception ex)
				{
					logInfo("error %s", ex.msg);
				}
			logInfo("%s", msg);
	    }
	
	return 0;
}

@s-ludwig
Copy link
Member

s-ludwig commented Jun 7, 2024

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

3 participants