Sails Controller 会为ajax请求返回正确的响应,但是当通过io.socket调用时,它将无法正常工作。

套接字请求:

io.socket.request({ method: 'get', url: '/example-service/', params: {}, headers:{}}, function(body, JWR){ console.log(JWR); })

异常(exception):
Error: Cannot write to response more than once\n    at onlyAllowOneResponse
node_modules/sails/lib/router/res.js:375:11
at MockServerResponse.send_shim [as send]
node_modules/sails/lib/router/res.js:192:7)

浏览器上的套接字响应:
JWR {body: "Not implemented in core yet", headers: Object, statusCode: 501, error: "Not implemented in core yet"}

根据sailsjs文档的请求/响应,将自动为websocket处理。

我在这里想念什么吗?对于套接字请求,我是否需要以其他方式发送响应?

最佳答案

异常是因为我使用的是res.render,如本文档中所述,帆在websockets中不受支持:

http://sailsjs.org/documentation/reference/response-res

通过替换来解决此问题

res.render


sails.hooks.views.render

10-06 16:00