[WebSocket] Calculating Ping?

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
What's the best way to calculate the average ping of a connection, specifically one that is WebSocket-based?
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
Well, that gives me a string ('NaN'), so I tried Date.now();, but it returns negative integers. Is that bad?
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
Edit: Well it magically started working. Thanks. I'd +rep, but it says I need to spread some around first. :/
 

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
I'm not a websocket pro but if you want to get the ping on the client side, you check the date then you send the request and when you receive the answer, you check the date again. From what i've seen thats not what your script does.
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
It's not too fancy:

PHP:
(function(window) {

	var socket = new WebSocket('ws://thelifelessone.dyndns.org:1024/');
	var input = document.getElementById('input');
	var output = document.getElementById('output');
	var msgCounter = 0;
	var ping;

	setInterval(function() {
		msgCounter--;
	}, 2000);

	socket.onopen = function(e) {
		output.innerText += 'Connected.\n';
	}

	socket.onmessage = function(e) {
		var dJSON = JSON.parse(e.data);
		ping = (dJSON.timestamp - Date.now());
		console.log(Math.abs(ping));
		
		if (dJSON.content != undefined) {
			output.innerText += dJSON.id + ': ' + dJSON.content + '\n';
		}
		window.scroll(0, document.height);
	}

	input.onkeypress = function(e) {
		if (e.keyCode == 13) {
			if (input.value != '') {
				switch(input.value) {
					case ':clear':
						output.innerText = '';
						input.value = '';
						break;
						
					default:
						msg = JSON.stringify({'timestamp': Date.now(), 'content': input.value});
						if (msgCounter != 5) {
							msgCounter++;
							socket.send(msg);
						}
						input.value = '';
						break;
				}
			}
		}
	}

})(window);

Server:
PHP:
// Requires Node.js with websocket-server.
var ws = require("websocket-server");
var server = ws.createServer();



server.addListener("connection", function(connection){
	console.log('Connection established. ID ' + connection.id + ' assigned.');
	connection.send(JSON.stringify({'id': connection.id}));
	
	connection.addListener('close', function(e) {
		console.log('Client ID ' + connection.id + ' has disconnected.');
	});

	connection.addListener("message", function(msg){
		msg = JSON.parse(msg);
		console.log(msg.timestamp);
			switch(msg) {
				case ':clear':
					break;
				default:
					connection.send(JSON.stringify({
						'id': connection.id,
						'content': msg.content,
						'timestamp': msg.timestamp
					}));
					connection.broadcast(JSON.stringify({
						'id': connection.id,
						'content': msg.content
					}));	
					break;
			}
	});

});

server.listen(1024);
console.log('Listening on port 1024');

It's still a work-in-progress, but it works.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top