在sails v0.11(客户端)中发出消息 [英] Emitting a message in sails v0.11 (client-side)

查看:46
本文介绍了在sails v0.11(客户端)中发出消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用最新版本的sails.js (0.11.0) 时遇到了一些问题.它在 github 中声明纯 socket.io 代码将被接受并在 Sails.js 中运行;然而,我只是想在客户点击类似的东西时发出一条消息:

I'm having some issues with the newest version of sails.js (0.11.0). It stated in github that plain socket.io code will be accepted and ran in sails.js; however, I am simply trying to emit a message from a client when they click on something like so:

$('#myBtn').on('click', function(){
     io.socket.emit('message', {  
         message: {
             subject: subject
         },
         sender: id
     });  

});

我最终在 io.socket.emit() aka emit is not a function ofio.socket.

I end up getting an "Uncaught TypeError: undefined is not a function" on the line of io.socket.emit() aka emit is not a function of io.socket.

以下是我看过的一些参考资料:

Here are some references that I have looked at:

我对sails的更新版本有一种感觉,而不是发出一条消息,我应该做一些类似的事情:

I have a feeling with the updated version of sails, instead of emitting a message I should be doing something along the lines of:

io.socket.post('/user/message', data, function(data, jwres) {

});

这里的回答让我有些担心:

Something concerns me with the following answer here:

声明教室"与publishCreatepublishDestroyintroduceobituary 一起被弃用.

It states "class rooms" are being deprecated along with publishCreate, publishDestroy, introduce, and obituary.

我是否遵循 Pub/Sub 范式,重写我更多的socket-io-ish"代码以利用风帆蓝图 &Pub/Sub,还是继续我的 socket-io 方式?

So do I follow a Pub/Sub paradigm, re-write my more "socket-io-ish" code to utilize sails Blueprints & Pub/Sub, or continue in my socket-io fashion?

是否有另一种使用风帆从客户端发出消息的方法?

Is there another way of emitting a message from client using sails?

推荐答案

您是对的,推荐的通过套接字与服务器通信的方式是使用 RESTful 套接字客户端方法.好处是您可以使用常规 Sails 路由和控制器/操作架构进行套接字通信,而不必在后端支持整个其他层的订阅和事件处理.这是@mikermcneil 最初创建 Sails 的主要原因之一.有两点需要注意:

You are correct in that the recommended way of communicating with the server via sockets is to use the RESTful socket client methods. The benefit is that you can use the regular Sails routing and controller/action architecture for socket communication instead of having to support a whole other layer of subscription and event-handling on the backend. This is one of the main reasons why @mikermcneil originally created Sails. Two things to note:

  1. 您可以使用 req.isSocket 在您的控制器操作中确定请求是否来自套接字,以及
  2. 您可以使用 io.socket._raw 在客户端上获取原始的底层 socket.io 实例,该实例将具有 emit 方法.但同样,这不是推荐的做法.
  1. You can use req.isSocket in your controller action to determine whether the request is coming from a socket, and
  2. You can get the raw, underlying socket.io instance on the client with io.socket._raw, which will have the emit method. But again, this is not the recommended practice.

这篇关于在sails v0.11(客户端)中发出消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆