diff options
| author | Jules Laplace <jules@okfoc.us> | 2012-09-24 16:22:07 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2012-09-24 16:22:07 -0400 |
| commit | 686106d544ecc3b6ffd4db2b665d3bc879a58d8c (patch) | |
| tree | a5b5e50237cef70e12f0745371896e96f5f6d578 /node_modules/socket.io/examples/irc-output | |
ok
Diffstat (limited to 'node_modules/socket.io/examples/irc-output')
5 files changed, 345 insertions, 0 deletions
diff --git a/node_modules/socket.io/examples/irc-output/app.js b/node_modules/socket.io/examples/irc-output/app.js new file mode 100644 index 0000000..784886a --- /dev/null +++ b/node_modules/socket.io/examples/irc-output/app.js @@ -0,0 +1,74 @@ +/** + * Module dependencies. + */ + +var express = require('express') + , stylus = require('stylus') + , nib = require('nib') + , sio = require('../../lib/socket.io') + , irc = require('./irc'); + +/** + * App. + */ + +var app = express.createServer(); + +/** + * App configuration. + */ + +app.configure(function () { + app.use(stylus.middleware({ src: __dirname + '/public', compile: compile })) + app.use(express.static(__dirname + '/public')); + app.set('views', __dirname); + app.set('view engine', 'jade'); + + function compile (str, path) { + return stylus(str) + .set('filename', path) + .use(nib()); + }; +}); + +/** + * App routes. + */ + +app.get('/', function (req, res) { + res.render('index', { layout: false }); +}); + +/** + * App listen. + */ + +app.listen(3000, function () { + var addr = app.address(); + console.log(' app listening on http://' + addr.address + ':' + addr.port); +}); + +/** + * Socket.IO server + */ + +var io = sio.listen(app) + +/** + * Connect to IRC. + */ + +var client = new irc.Client('irc.freenode.net', 6667); +client.connect('socketio\\test\\' + String(Math.random()).substr(-3)); +client.on('001', function () { + this.send('JOIN', '#node.js'); +}); +client.on('PART', function (prefix) { + io.sockets.emit('announcement', irc.user(prefix) + ' left the channel'); +}); +client.on('JOIN', function (prefix) { + io.sockets.emit('announcement', irc.user(prefix) + ' joined the channel'); +}); +client.on('PRIVMSG', function (prefix, channel, text) { + io.sockets.emit('irc message', irc.user(prefix), text); +}); diff --git a/node_modules/socket.io/examples/irc-output/index.jade b/node_modules/socket.io/examples/irc-output/index.jade new file mode 100644 index 0000000..5468632 --- /dev/null +++ b/node_modules/socket.io/examples/irc-output/index.jade @@ -0,0 +1,28 @@ +doctype 5 +html + head + link(href='/stylesheets/style.css', rel='stylesheet') + script(src='http://code.jquery.com/jquery-1.6.1.min.js') + script(src='/socket.io/socket.io.js') + script + var socket = io.connect(); + + socket.on('connect', function () { + $('#irc').addClass('connected'); + }); + + socket.on('announcement', function (msg) { + $('#messages').append($('<p>').append($('<em>').text(msg))); + $('#messages').get(0).scrollTop = 10000000; + }); + + socket.on('irc message', function (user, msg) { + $('#messages').append($('<p>').append($('<b>').text(user), msg)); + $('#messages').get(0).scrollTop = 10000000; + }); + body + h2 Node.JS IRC + #irc + #connecting + .wrap Connecting to socket.io server + #messages diff --git a/node_modules/socket.io/examples/irc-output/irc.js b/node_modules/socket.io/examples/irc-output/irc.js new file mode 100644 index 0000000..cc679d5 --- /dev/null +++ b/node_modules/socket.io/examples/irc-output/irc.js @@ -0,0 +1,164 @@ +/** + * From https://github.com/felixge/nodelog/ + */ + +var sys = require('util'); +var tcp = require('net'); +var irc = exports; + +function bind(fn, scope) { + var bindArgs = Array.prototype.slice.call(arguments); + bindArgs.shift(); + bindArgs.shift(); + + return function() { + var args = Array.prototype.slice.call(arguments); + fn.apply(scope, bindArgs.concat(args)); + }; +} + +function each(set, iterator) { + for (var i = 0; i < set.length; i++) { + var r = iterator(set[i], i); + if (r === false) { + return; + } + } +} + +var Client = irc.Client = function(host, port) { + this.host = host || 'localhost'; + this.port = port || 6667; + + this.connection = null; + this.buffer = ''; + this.encoding = 'utf8'; + this.timeout = 10 * 60 * 60 * 1000; + + this.nick = null; + this.user = null; + this.real = null; +} +sys.inherits(Client, process.EventEmitter); + +Client.prototype.connect = function(nick, user, real) { + var connection = tcp.createConnection(this.port, this.host); + connection.setEncoding(this.encoding); + connection.setTimeout(this.timeout); + connection.addListener('connect', bind(this.onConnect, this)); + connection.addListener('data', bind(this.onReceive, this)); + connection.addListener('end', bind(this.onEof, this)); + connection.addListener('timeout', bind(this.onTimeout, this)); + connection.addListener('close', bind(this.onClose, this)); + + this.nick = nick; + this.user = user || 'guest'; + this.real = real || 'Guest'; + + this.connection = connection; +}; + +Client.prototype.disconnect = function(why) { + if (this.connection.readyState !== 'closed') { + this.connection.close(); + sys.puts('disconnected (reason: '+why+')'); + this.emit('DISCONNECT', why); + } +}; + +Client.prototype.send = function(arg1) { + if (this.connection.readyState !== 'open') { + return this.disconnect('cannot send with readyState: '+this.connection.readyState); + } + + var message = []; + for (var i = 0; i< arguments.length; i++) { + if (arguments[i]) { + message.push(arguments[i]); + } + } + message = message.join(' '); + + sys.puts('> '+message); + message = message + "\r\n"; + this.connection.write(message, this.encoding); +}; + +Client.prototype.parse = function(message) { + var match = message.match(/(?:(:[^\s]+) )?([^\s]+) (.+)/); + var parsed = { + prefix: match[1], + command: match[2] + }; + + var params = match[3].match(/(.*?) ?:(.*)/); + if (params) { + // Params before : + params[1] = (params[1]) + ? params[1].split(' ') + : []; + // Rest after : + params[2] = params[2] + ? [params[2]] + : []; + + params = params[1].concat(params[2]); + } else { + params = match[3].split(' '); + } + + parsed.params = params; + return parsed; +}; + +Client.prototype.onConnect = function() { + this.send('NICK', this.nick); + this.send('USER', this.user, '0', '*', ':'+this.real); +}; + +Client.prototype.onReceive = function(chunk) { + this.buffer = this.buffer + chunk; + + while (this.buffer) { + var offset = this.buffer.indexOf("\r\n"); + if (offset < 0) { + return; + } + + var message = this.buffer.substr(0, offset); + this.buffer = this.buffer.substr(offset + 2); + sys.puts('< '+message); + + message = this.parse(message); + + this.emit.apply(this, [message.command, message.prefix].concat(message.params)); + + if (message !== false) { + this.onMessage(message); + } + } +}; + +Client.prototype.onMessage = function(message) { + switch (message.command) { + case 'PING': + this.send('PONG', ':'+message.params[0]); + break; + } +}; + +Client.prototype.onEof = function() { + this.disconnect('eof'); +}; + +Client.prototype.onTimeout = function() { + this.disconnect('timeout'); +}; + +Client.prototype.onClose = function() { + this.disconnect('close'); +}; + +exports.user = function(prefix) { + return prefix.match(/:([^!]+)!/)[1] +}; diff --git a/node_modules/socket.io/examples/irc-output/package.json b/node_modules/socket.io/examples/irc-output/package.json new file mode 100644 index 0000000..665ee33 --- /dev/null +++ b/node_modules/socket.io/examples/irc-output/package.json @@ -0,0 +1,10 @@ +{ + "name": "socket.io-irc" + , "version": "0.0.1" + , "dependencies": { + "express": "2.5.5" + , "jade": "0.16.4" + , "stylus": "0.19.0" + , "nib": "0.2.0" + } +} diff --git a/node_modules/socket.io/examples/irc-output/public/stylesheets/style.styl b/node_modules/socket.io/examples/irc-output/public/stylesheets/style.styl new file mode 100644 index 0000000..2713512 --- /dev/null +++ b/node_modules/socket.io/examples/irc-output/public/stylesheets/style.styl @@ -0,0 +1,69 @@ +@import 'nib' + +h2 + font bold 18px Helvetica Neue, Arial + +#irc, #messages + width 600px + +#irc + position relative + border 1px solid #ccc + +#connecting + position absolute + height 410px + z-index 100 + left 0 + top 0 + background #fff + text-align center + width 600px + font 15px Georgia + color #666 + display block + .wrap + padding-top 150px + +.connected + #connecting + display none + +#messages + height 380px + background #eee + overflow auto + overflow-x hidden + overflow-y auto + &::-webkit-scrollbar + width 6px + height 6px + &::-webkit-scrollbar-button:start:decrement, ::-webkit-scrollbar-button:end:increment + display block + height 10px + &::-webkit-scrollbar-button:vertical:increment + background-color #fff + &::-webkit-scrollbar-track-piece + background-color #fff + -webkit-border-radius 3px + &::-webkit-scrollbar-thumb:vertical + height 50px + background-color #ccc + -webkit-border-radius 3px + &::-webkit-scrollbar-thumb:horizontal + width 50px + background-color #fff + -webkit-border-radius 3px + em + text-shadow 0 1px 0 #fff + color #999 + p + padding 0 + margin 0 + font 12px Helvetica, Arial + padding 5px 10px + b + display inline-block + padding-right 10px + p:nth-child(even) + background #fafafa |
