【问题标题】:Routing with restify and socket.io in node在节点中使用 restify 和 socket.io 进行路由
【发布时间】:2013-11-18 23:14:52
【问题描述】:

我有一个节点应用程序,它使用 restfy 根据 url 路由请求。

现在想将套接字放入图片中。但是当使用 socket.io 时,应用程序不会重定向到该函数。我在 flex 中使用 FlexSocket.IO 库。

这是我的代码 sn-p。

// 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');
});

在弹性中

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.
}

代码有什么问题吗?为什么node中没有调用socket.handle函数?

【问题讨论】:

  • 尝试配置,服务器上只有“flashsocket”
  • 是的,试过了,但还是不行。它仅在我将 io.sockets.on('connection', function (client) { var flexClient = client; .... 部分放入 app.js 文件时才有效。 app.get 和 app.post 似乎没有工作。
  • 尽量不要导出变量,而是将它们保存在app.js中,不要作为中间件运行,让客户端连接到服务器端口,这样它就可以切换到websocket服务器。使用命名空间 .of('/') 和其他命名空间发送到其在 / 路由上的客户端。
  • @GeoPhoenix 感谢回复。我只想在调用特定 url 时使用套接字连接。我尝试在节点上使用io.of("/socket").on("connection", function (socket) { ---- });。和 socket = new FlashSocket("localhost:8080/socket"); 在 flex 端,但是 flex 应用程序无法连接到套接字并给出发现错误。我猜我使用 FlexSocket 连接的方式有问题。有人可以帮忙吗?

标签: node.js apache-flex socket.io restify flashsocket


【解决方案1】:

在 app.js 中,尝试

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

【讨论】:

    猜你喜欢
    • 2014-01-31
    • 2020-02-25
    • 1970-01-01
    • 2021-03-04
    • 2016-10-18
    • 2018-06-17
    • 2012-05-27
    • 2018-01-06
    • 1970-01-01
    相关资源
    最近更新 更多