服务器:sails.js(0.11.x)是服务器
客户端:一个带有sails.io@0.11.5和socket.io-client@1.3.5的node.js脚本
大图:我已经拥有或将拥有一个连接到sails.js服务器并将执行各种任务的node.js脚本.
直接目标:我想在客户端 - >服务器的套接字连接期间发出一个事件,例如:
socket.emit('got_job', job.id);
Run Code Online (Sandbox Code Playgroud)
为什么?如果可能,我可以在一个控制器(或控制器+服务)中在服务器端创建各种事件处理程序,并在管理客户端/服务器端点之间的一组有状态事务时保持我的代码清洁,以支持此脚本服务器场.
文档:这是如何使用socket.io-client为sails.js这个每个sails文档:https://github.com/balderdashy/sails.io.js? files = 1# for-nodejs
我没有太多的代码可以分享除了链接中的内容,但我会在这里粘贴以防万一:
var socketIOClient = require('socket.io-client');
var sailsIOClient = require('sails.io.js');
// Instantiate the socket client (`io`)
// (for now, you must explicitly pass in the socket.io client when using this library from Node.js)
var io = sailsIOClient(socketIOClient);
// Set some options:
// (you have to specify the host and port of the Sails backend when using this library from …Run Code Online (Sandbox Code Playgroud)