Created
January 25, 2013 08:30
-
-
Save shigeki/4632789 to your computer and use it in GitHub Desktop.
socket.io を使ったデータの受け渡し (HTTP GET -> WS リレーサーバ対応)
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Standalone.sample</title> | |
<script src="http://localhost:8080/socket.io/socket.io.js"></script> | |
<script type="text/javascript"> | |
var appname = io.connect('http://localhost:8080/appname'); | |
appname.on('available_channel', function(channels) { | |
console.log('channels', channels); | |
// リスト先頭のチャネルIDを利用する | |
appname.emit('join', channels[0]); | |
appname.once('joined', function() { | |
appname.emit('post', 'hello!!'); | |
}); | |
}); | |
appname.on('user:message', function (query) { | |
console.log('user:message', query); | |
var queryDOM = document.getElementById('query'); | |
// Firefox向け処理 | |
if (queryDOM.textContent) { | |
queryDOM.textContent = query['1'] + ' & ' + query['2']; | |
} else { | |
queryDOM.innerHTML = query['1'] + ' & ' + query['2']; | |
} | |
}); | |
</script> | |
</head> | |
<body> | |
<h1>Alert From Batch</h1> | |
<dl> | |
<dt>query</dt> | |
<dd id="query"></dd> | |
</dl> | |
</body> | |
</html> |
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
var http = require('http'); | |
var fs = require('fs'); | |
var url = require('url'); | |
var html = fs.readFileSync(__dirname + '/index.html'); | |
function RoomServer(opts) { | |
// デフォルトのリスンポートは 8080 | |
this.port = opts.port || 8080; | |
function onRequest(req, res) { | |
var parse = url.parse(req.url, true); | |
var query = parse.query; | |
// 監視エージェントから来たリクエストは処理しない | |
if (query['1'] || query['2']) return; | |
res.end(html); | |
} | |
this.server = http.createServer(onRequest).listen(this.port); | |
var io = require('socket.io').listen(this.server); | |
var channels = ['Appname']; | |
var appname = io.of('/appname'); | |
appname.on('connection', function(socket) { | |
console.log('connected: %s', socket.id); | |
socket.emit('available_channel', channels); | |
socket.on('join', function(channelId) { | |
console.log('%s joined channel: %s', socket.id, channelId); | |
socket.join(channelId); | |
socket.set('channelId', channelId); | |
socket.emit('joined'); | |
}); | |
socket.on('post', function(message) { | |
socket.get('channelId', function(err, channelId) { | |
console.log('post %s says<%s channel>: %s', | |
socket.id, channelId, message); | |
}); | |
}); | |
socket.on('user:message', function(message) { | |
socket.get('channelId', function(err, channelId) { | |
console.log('user:message %s says<%s channel>: %s', | |
socket.id, channelId, message); | |
appname.in(channelId).emit('user:message', message); | |
}); | |
}); | |
socket.on('disconnect', function() { | |
console.log('%s disconnected', socket.id); | |
socket.get('channelId', function(channelId) { | |
socket.leave(channelId); | |
}); | |
}); | |
}); | |
} | |
exports.RoomServer = RoomServer; |
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
var url = require('url'); | |
// socket.io のデータを配信するサーバ | |
var RoomServer = require('./roomserver').RoomServer; | |
// 監視エージェントの HTTP GET を WS にリレーするサーバ | |
var WsRelay = require('./wsrelay').WsRelay; | |
function start(route, handle) { | |
var roomOpt = {port: 8080}; | |
var roomServer = new RoomServer(roomOpt); | |
var dest = {protocol: 'http:', hostname: 'localhost', | |
port: roomOpt.port, pathname: '/appname'}; | |
var wsOpt = { server: roomServer.server, dest: url.format(dest)}; | |
var wsRelay = new WsRelay(route, null, wsOpt); | |
} | |
exports.start = start; | |
function route(handle, pathname, res) { | |
console.log('pathname:', pathname); | |
} | |
start(route, null); |
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
var http = require('http'); | |
var url = require('url'); | |
var io = require('socket.io-client'); | |
function WsRelay(route, handle, opts) { | |
var self = this; | |
// socket.io の接続先 | |
this.dest = opts.dest || 'http://localhost:8080/appname'; | |
// サーバハンドラが渡されたそちらを優先して利用する | |
if (opts.server) { | |
this.server = opts.server; | |
} else { | |
// デフォルトのリスンポート 8081 | |
this.port = opts.port || 8081; | |
} | |
function onRequest(req, res) { | |
var parse = url.parse(req.url, true); | |
var query = parse.query; | |
// 監視エージェントからのリクエストのみ処理する | |
if (!query['1'] && !query['2']) return; | |
// 初回リクエストで socket.io 接続 | |
if (!self.appname) { | |
self.appname = io.connect(self.dest); | |
} | |
var appname = self.appname; | |
appname.on('available_channel', function(channels) { | |
console.log('avaiable_channnel', channels); | |
// リスト先頭のチャネルIDを利用する | |
appname.emit('join', channels[0]); | |
}); | |
appname.on('connect', function() { | |
console.log('connect'); | |
self.connected = true; | |
appname.once('joined', function() { | |
// 初回リクエスト時のデータ送信 | |
appname.json.emit('user:message', query); | |
}); | |
}); | |
// 既に socket.io 接続されている時のデータ送信 | |
if (self.connected) appname.json.emit('user:message', query); | |
req.setEncoding('utf8'); | |
req.on('end', function() { | |
route(handle, parse.pathname, res); | |
}); | |
res.end(); | |
} | |
// サーバハンドラが存在していればそちらを優先する | |
if (this.server) { | |
this.server.on('request', onRequest); | |
} else { | |
http.createServer(onRequest).listen(this.port); | |
} | |
} | |
exports.WsRelay = WsRelay; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment