var socket = new WebSocket("ws://echo.websocket.org/"),
pubsub = new Mediator();
socket.onopen = function() {
pubsub.Publish("chat/open")
};
socket.onclose = function() {
pubsub.Publish("chat/close")
};
socket.onerror = function() {
pubsub.Publish("chat/error")
};
socket.onmessage = function(data) {
pubsub.Publish(data.Type.join(":"), data);
};
pubsub.Subscribe("chat:message:send", function(data) {
socket.send(data);
});
pubsub.Subscribe("chat:message:receive", function(data){
console.log(data);
});
pubsub.Subscribe("chat:open", function() {
console.log("Socket opened.");
pubsub.publish("chat:message:send", "Hi, server!");
});
pubsub.Subscribe("chat", function(data){
alertPlayerForChatMessage(data.message);
}, function(data){
return data.name == player.name;
});
pubsub.Subscribe("game:environment:status", function(data){
console.log("It has started to " + data.weatherType);
});
External resources loaded into this fiddle: