码迷,mamicode.com
首页 > 其他好文 > 详细

Routing with restify and socket.io in node

时间:2014-11-22 07:08:20      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:http   io   ar   os   sp   on   log   cti   bs   


I have a node app which routes the request based on the url using restfy.

Now in want to bring socket into picture. But app doesnot redirect to the function when when socket.io is used. I am using FlexSocket.IO library in flex.

Here‘s my code snippet.

// In app.js 
var restify = require(‘restify‘)
, http = require(‘http‘)
,socket = require(‘./routes/socket‘);

var app = restify.createServer();

app.get(‘/‘, socket.handle);
app.post(‘/‘, socket.handle);

var io = require(‘socket.io‘).listen(app);

app.listen(8080, function() {
  console.log(‘%s listening at %s‘, app.name, app.url);
});
io.configure(function() {
  io.set(‘transports‘, [‘websocket‘,‘flashsocket‘]);
  io.set(‘flash policy port‘, 843);
});
exports.io = io;

//In socket.js
exports.handle = function (req, res, next) {
console.log(‘In Handle‘); //doesn‘t print this
io.sockets.on(‘connection‘, function(client){
console.log(‘Connection establiished‘);
});

In Flex

private var socket:FlashSocket;
socket = new FlashSocket("localhost:8080");
socket.addEventListener(FlashSocketEvent.CONNECT, onConnect); 
socket.addEventListener(FlashSocketEvent.MESSAGE, onMessage);

protected function onConnect(event:FlashSocketEvent):void 
{
  Alert.show(‘connect‘); //This alert is shown.
}

Is there anything wrong with the code? Why is the socket.handle function not called in node?


Answer 

In app.js, try

var io = require(‘socket.io‘).listen(app.server); //app.server instead of just app

注:可能按照官网上的代码 会导致restify运行出错,按照该修改socket.io监听server才可以


Routing with restify and socket.io in node

标签:http   io   ar   os   sp   on   log   cti   bs   

原文地址:http://my.oschina.net/u/574928/blog/347388

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!