Created
November 8, 2010 03:12
-
-
Save Vagabond/667339 to your computer and use it in GitHub Desktop.
Ape socket server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ape.addEvent("init", function() { | |
var socket = new Ape.sockServer(1337, "0.0.0.0", {flushlf: true}); | |
socket.onAccept = function(client) { | |
Ape.log("Client connected"); | |
} | |
socket.onRead = function(client, data) { | |
var parts = data.split(' '); | |
switch (parts[0]) { | |
case 'ADDMESSAGE': | |
var channelname = parts[1]; | |
var time = parts[2]; | |
var message = parts.slice(3).join(" "); | |
var channel = Ape.getChannelByName(channelname); | |
if (!channel) { | |
//Ape.log("no " + channelname + " channel, creating it"); | |
//channel = Ape.mkChan(channelname); | |
return; // nobody is subscribed, dump it | |
} | |
//Ape.log("Got " + channelname); | |
channel.pipe.sendRaw('MSG', {message: JSON.parse(message), channel: channelname, time: parseInt(time)}); | |
break; | |
default: | |
Ape.log("Unhandled message: " + data); | |
break; | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment