简体   繁体   English

在节点中使用Restify和socket.io进行路由

[英]Routing with restify and socket.io in node

I have a node app which routes the request based on the url using restfy. 我有一个节点应用程序,使用restfy根据URL路由请求。

Now in want to bring socket into picture. 现在要把插座变成图片。 But app doesnot redirect to the function when when socket.io is used. 但是当使用socket.io时,app不会重定向到该函数。 I am using FlexSocket.IO library in flex. 我在flex中使用FlexSocket.IO库。

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 在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? 为什么未在节点中调用socket.handle函数?

在app.js中,尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM