Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add an example for a chat room, where the router routes each message …

…to all of the clients
  • Loading branch information...
commit 022b786fbb9f753e098a5412c4420b737ef5ea51 1 parent cbac0db
@towski towski authored
Showing with 72 additions and 0 deletions.
  1. +72 −0 irc.js
View
72 irc.js
@@ -0,0 +1,72 @@
+// IM chat
+// Binds a ROUTER socket on 5555
+
+require('tty').setRawMode(true);
+
+var context = require('zeromq')
+ , router = context.createSocket('router')
+ , request = context.createSocket('request')
+ , reply = context.createSocket('reply')
+ , name = process.env.NAME || process.env.USER
+ , connect_port = process.env.CONNECT_PORT || '5555'
+ , serve_port = process.env.SERVE_PORT || '5555'
+
+var peers = []
+
+router.on('message', function(from, msg) {
+ if(server){
+ var sender = from.toString();
+ if(peers.indexOf(sender) == -1){
+ peers.push(sender)
+ }
+ for(peer in peers){
+ router.send(peers[peer], sender + " " + msg.toString())
+ }
+ } else {
+ console.log(msg.toString());
+ }
+})
+
+router.identity = name
+console.log("Logging in as", name)
+
+var message = '',
+ lastuser = "towski",
+ match = null,
+ server = false
+
+process.stdin.on("data", function(buf) {
+ if(buf[0] == 13) {
+ var text = message.trim()
+ message = ''
+ if(text == '') return
+ if(text == 'q') {
+ console.log("Closing...")
+ process.stdin.pause()
+ router.close()
+ process.kill()
+ } else {
+ if(server){
+ for(peer in peers){
+ router.send(peer, text)
+ }
+ } else {
+ router.send("towski", text)
+ }
+ }
+ } else {
+ message += buf.toString()
+ }
+})
+
+if(process.env.SERVE_PORT){
+ server = true
+ router.bind("tcp://*:5555", function() {
+ process.stdin.resume()
+ })
+} else {
+ router.connect("tcp://localhost:5555")
+ process.stdin.resume()
+}
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.