HTML and CSS Reference
In-Depth Information
Listing 11-15. All Message Types in Rawkets, in the File main.js
{type: <MESSAGE TYPE ID>, arg1: value1, arg2: value2, ...}
/**
* Message protocols
*/
var MESSAGE_TYPE_PING = 1;
var MESSAGE_TYPE_UPDATE_PING = 2;
var MESSAGE_TYPE_NEW_PLAYER = 3;
var MESSAGE_TYPE_SET_COLOUR = 4;
var MESSAGE_TYPE_UPDATE_PLAYER = 5;
var MESSAGE_TYPE_REMOVE_PLAYER = 6;
var MESSAGE_TYPE_AUTHENTICATION_PASSED = 7;
var MESSAGE_TYPE_AUTHENTICATION_FAILED = 8;
var MESSAGE_TYPE_AUTHENTICATE = 9;
var MESSAGE_TYPE_ERROR = 10;
var MESSAGE_TYPE_ADD_BULLET = 11;
var MESSAGE_TYPE_UPDATE_BULLET = 12;
var MESSAGE_TYPE_REMOVE_BULLET = 13;
var MESSAGE_TYPE_KILL_PLAYER = 14;
var MESSAGE_TYPE_UPDATE_KILLS = 15;
var MESSAGE_TYPE_REVIVE_PLAYER = 16;
socket = ws.createServer();
serverStart = new Date().getTime();
players = [];
bullets = [];
// On incoming connection from client
socket.on("connection", function(client) {
socket.broadcast(formatMessage(MESSAGE_TYPE_REMOVE_BULLET, {i: bulletId}));
socket.broadcast(formatMessage(MESSAGE_TYPE_KILL_PLAYER, {i: player.id}));
socket.broadcast(formatMessage(MESSAGE_TYPE_UPDATE_KILLS, {i: bulletPlayer.id,
k: bulletPlayer.killCount}));
socket.broadcast(formatMessage(MESSAGE_TYPE_UPDATE_BULLET, {i: bullet.id,
x: bullet.x, y: bullet.y}));
// omitted code
});
The helper function formatMessage converts the arguments into a JavaScript object and binary encodes it before
passing it to the socket to broadcast to other clients:
function formatMessage(type, args) {
var msg = {type: type};
Search WWH ::




Custom Search