2

我有一个 Node.js / Express 应用程序正在运行,它接收如下路由:

app.get('/resource/:res', someFunction);
app.get('/foo/bar/:id', someOtherFunction);

这很棒,而且效果很好。

我也在使用 Socket.IO,并且想让一些服务器调用使用 websockets 而不是传统的 RESTful 调用。但是,我想让它非常干净并且几乎使用相同的语法,最好是:

app.sio.get('/resource/:res', someFunction);

这将为 Socket.IO 提供一个合成的“REST”接口,从程序员的角度来看,他没有做任何不同的事情。只是websockets: true从客户端标记。

我可以处理所有细节,例如传入请求动词并解析它们的自定义方式等等,我对此没有问题。我唯一要寻找的是一些可以像 express 一样解析路由并正确路由它们的函数。例如,

// I don't know how to read the ':bar',
'foo/:bar'

// Or handle all complex routings, such as
'foo/:bar/and/:so/on'

我可以深入挖掘并尝试自己编写代码,或者尝试通读所有 express 的源代码并找到他们在哪里执行它,但我确信它本身就存在。只是不知道在哪里可以找到它。

更新

robertklep 提供了一个很好的答案,完全为我解决了这个问题。我将其改编为完整的解决方案,并在下面的答案中发布。

4

2 回答 2

3

robertklep 提供了一个很好的答案,完全为我解决了这个问题。我将它改编成一个完整的解决方案,以防其他人想做类似的事情:

节点(服务器端):

// Extend Express' Router to a simple name
app.sio = new express.Router();
app.sio.socketio = require('socket.io').listen(server, { log: false });

// Map all sockets requests to HTTP verbs, which parse
// the request and pass it into a simple callback.
app.sio.socketio.sockets.on('connection', function (socket) {
  var verbs = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];
  for (var i = 0; i < verbs.length; ++i) {
    var go = function(verb) {
      socket.on(verb, function (url, data) {
        var route = app.sio.match(verb, url);
        if (route && route.callbacks.length) {
          var req = {url: url, params: route.params, data: data, socket:socket}
          route.callbacks[0](req);
        }  
      });
    }(verbs[i]);
  }
});

// Simplify Socket.IO's 'emit' function and liken
// it traditional Express routing.
app.sio.end = function(req, res) {
  req.socket.emit('response', req.url, res);
}

// Here's an example of a simplified request now, which 
// looks nearly just like a traditional Express request.
app.sio.get('/foo/:bar', function(req) {
  app.sio.end(req, 'You said schnazzy was ' + req.data.schnazzy);
});

客户端:

// Instantiate Socket.IO
var socket = io.connect('http://xxxxxx');
socket.callbacks = {};

// Similar to the server side, map functions
// for each 'HTTP verb' request and handle the details.    
var verbs = ['get', 'post', 'put', 'path', 'delete'];
for (var i = 0; i < verbs.length; ++i) {
  var go = function(verb) {
    socket[verb] = function(url, data, cb) {
      socket.emit(String(verb).toUpperCase(), url, data);
      if (cb !== undefined) {
        socket.callbacks[url] = cb; 
      }
    }
  }(verbs[i]);
}

// All server responses funnel to this function,
// which properly routes the data to the correct
// callback function declared in the original request.
socket.on('response', function (url, data) {
  if (socket.callbacks[url] != undefined) {
    socket.callbacks[url](data);
  }
});

// Implementation example, params are:
// 1. 'REST' URL,
// 2. Data passed along,
// 3. Callback function that will trigger
//    every time this particular request URL
//    gets a response.
socket.get('/foo/bar', { schnazzy: true }, function(data){
  console.log(data); // -> 'You said schnazzy was true'
});

谢谢你的帮助,罗伯特克莱普!

于 2013-10-20T02:52:55.493 回答
3

您可以使用 Express 路由器类来完成繁重的工作:

var io        = require('socket.io').listen(...);
var express   = require('express');
var sioRouter = new express.Router();

sioRouter.get('/foo/:bar', function(socket, params) {
  socket.emit('response', 'hello from /foo/' + params.bar);
});

io.sockets.on('connection', function(socket) {

  socket.on('GET', function(url) {
    // see if sioRouter has a route for this url:
    var route = sioRouter.match('GET', url);
    // if so, call its (first) callback (the route handler):
    if (route && route.callbacks.length) {
      route.callbacks[0](socket, route.params);
    }
  });

});

// client-side
var socket = io.connect();
socket.emit('GET', '/foo/helloworld');

您显然可以在请求中传递额外的数据并将其传递给您的路由处理程序(例如作为额外参数)。

于 2013-10-19T20:24:40.943 回答