Created
May 16, 2009 00:37
-
-
Save alexyoung/112520 to your computer and use it in GitHub Desktop.
This file contains 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
// JsChat client code using Rhino | |
// rlwrap java -jar js.jar | |
// load('jschat-client-rhino.js'); | |
var JsChat = {}; | |
JsChat.Connection = function(server_address, port) { | |
importPackage(java.io); | |
importPackage(java.net); | |
this.server_address = server_address; | |
this.port = port; | |
} | |
JsChat.Connection.prototype = { | |
sleep: function(milliseconds) { | |
java.lang.Thread.sleep(milliseconds); | |
}, | |
connect: function() { | |
print("Connecting to: " + this.server_address); | |
this.socket = new Socket(this.server_address, this.port); | |
this.connection_in = new BufferedReader(new InputStreamReader(this.socket.getInputStream())); | |
this.connection_out = new PrintWriter(this.socket.getOutputStream(), true); | |
}, | |
write: function(text) { | |
this.connection_out.println(text); | |
this.connection_out.flush(); | |
this.sleep(50); | |
}, | |
read: function() { | |
return this.connection_in.readLine(); | |
}, | |
close: function() { | |
this.connection_out.close(); | |
this.connection_in.close(); | |
this.socket.close(); | |
} | |
}; | |
JsChat.Protocol = function(connection) { | |
this.connection = connection; | |
} | |
JsChat.Protocol.prototype = { | |
identify: function(name) { | |
this.connection.write('{"identify":"' + name + '"}'); | |
}, | |
// Assumes a single room for now | |
join: function(room) { | |
this.room = room; | |
this.connection.write('{"join":"' + room + '"}'); | |
}, | |
message: function(text) { | |
this.connection.write('{"send":"' + text + '","to":"' + this.room + '"}'); | |
}, | |
quit: function() { | |
this.connection.close(); | |
} | |
}; | |
JsChat.Responses = {}; | |
JsChat.Responses.Display = { | |
'identified': function(json) { | |
print('Identified as: ' + json['name']); | |
}, | |
'join': function(json) { | |
print('Joined room: ' + json['room']); | |
}, | |
'error': function(json) { | |
print('[ERROR] ' + json['message']); | |
}, | |
'message': function(json) { | |
if (json['room']) { | |
print('[' + json['room'] + '] <' + json['user'] + '> ' + json['message']); | |
} | |
} | |
} | |
JsChat.Session = function(options) { | |
importClass(java.lang.Thread, java.lang.Runnable); | |
this.connection = new JsChat.Connection(options.server, options.port); | |
this.connection.connect(); | |
this.send = new JsChat.Protocol(this.connection); | |
this.user_responses = options.responses; | |
this.runReaderThread(); | |
} | |
JsChat.Session.prototype = { | |
quit: function() { | |
this.send.quit(); | |
}, | |
read: function() { | |
return this.connection.read(); | |
}, | |
handeUserResponses: function(json) { | |
for (var response in this.user_responses) { | |
var matcher = response; | |
var method = this.user_responses[response]; | |
var regex = new RegExp(matcher); | |
if (json['message'].match(matcher)) { | |
this.send.message(method(json)); | |
return true; | |
} | |
} | |
return false; | |
}, | |
runReaderThread: function() { | |
var session = this; | |
var r = new Runnable() { | |
run: function() { | |
var connected = true; | |
while (connected) { | |
try { | |
var json = session.readInput(); | |
if (json && json.display && JsChat.Responses.Display[json.display]) { | |
var options = json[json.display]; | |
JsChat.Responses.Display[json.display](options); | |
if (json.display == 'message') { | |
session.handeUserResponses(options); | |
} | |
} | |
} catch (exception) { | |
print(exception); | |
connected = false; | |
} | |
} | |
} | |
} | |
new Thread(r).start(); | |
}, | |
readInput: function() { | |
return eval('(' + this.read() + ')'); | |
} | |
}; | |
print('Connecting...'); | |
var Responses = { | |
'B\\^\\]': function(message) { | |
return message['user'] + ': hello'; | |
}, | |
'bye': function(message) { | |
return 'Bye ' + message['user']; | |
} | |
} | |
var jschat = new JsChat.Session({ server: 'irc.helicoid.net', port: 6789, responses: Responses }); | |
jschat.send.identify('B^]'); | |
jschat.send.join('#jschat'); | |
jschat.send.message("hello this is a test"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment