nod*_*arB 9 javascript node.js socket.io sails.js sails.io.js
服务器: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 Node.js)
io.sails.url = 'http://localhost:1337';
// ...
// Send a GET request to `http://localhost:1337/hello`:
io.socket.get('/hello', function serverResponded (body, JWR) {
// body === JWR.body
console.log('Sails responded with: ', body);
console.log('with headers: ', JWR.headers);
console.log('and with status code: ', JWR.statusCode);
// When you are finished with `io.socket`, or any other sockets you connect manually,
// you should make sure and disconnect them, e.g.:
io.socket.disconnect();
// (note that there is no callback argument to the `.disconnect` method)
});
Run Code Online (Sandbox Code Playgroud)
我已经研究过:我已经深入研究了这些物体的各个层次,我似乎无法找到任何可以使用的东西.并且只是尝试io.socket.emit(),因为它不存在.但是io.socket.get()和io.socket.post()等工作正常.
console.log(socketIOClient);
console.log(sailsIOClient);
console.log(io);
console.log(io.socket._raw);
console.log(io.sails);
Run Code Online (Sandbox Code Playgroud)
谢谢,我会根据需要尝试更新,以便澄清.
更新:
其他服务器信息 :
你很接近:
您的 io.socket.get 调用有点像rest api 调用。您需要一个绑定到 url '/hello' 上的 get 请求的 sails 控制器,
//client
io.socket.get('/hello', function serverResponded (body, JWR) {
//this is the response from the server, not a socket event handler
console.dir(body);//{message:"hello"}
});
Run Code Online (Sandbox Code Playgroud)
在
config/routes.js:
{
'get /hello':'MyController.hello'
}
//controllers/MyController
{
hello:function(req,res){
res.json({message:"hello"});
}
}
Run Code Online (Sandbox Code Playgroud)
您正在寻找的方法是 io.socket.on('eventName');
这是一个例子:
//client
io.socket.get('/hello', function serverResponded (body, JWR) {
//all we're doing now is subscribing to a room
console.dir(body);
});
io.socket.on('anevent',function(message){
console.dir(message);
});
in
config/routes.js:
{
'get /hello':'MyController.hello'
}
//controllers/MyController
{
hello:function(req,res){
sails.sockets.join(req.socket,'myroom');
res.json({message:'youve subscribed to a room'});
}
}
Run Code Online (Sandbox Code Playgroud)
我们有效地完成的是将套接字设置为“房间”的一部分,“房间”基本上是一个事件命名空间。然后,其他一些控制器只需执行以下操作:
sails.sockets.broadcast('myroom','anevent',{message:'socket event!'});
Run Code Online (Sandbox Code Playgroud)
进行此调用后,您将在 io.socket.on('anevent') 的回调中收到消息。
| 归档时间: |
|
| 查看次数: |
1881 次 |
| 最近记录: |