简体   繁体   English

无法从客户端发出套接字消息

[英]Cannot emit socket message from client

So, I'm setting up an express app, with all the usual goodies like passport, mongoose, connect-flash etc, and I'm using socket.io to listen for and emit messages. 因此,我正在设置一个Express应用程序,其中包含护照,猫鼬,connect-flash等所有常用的东西,并且我正在使用socket.io侦听和发出消息。

Currently, I'm just emitting messages to everyone (will be setting up rooms later on) and everything (but this) is working great. 目前,我只是向所有人发送消息(稍后将设置房间),并且所有操作(但此操作)都很好。 When an end user visits any page, all currently connected users receive a message "Hi everyone!", and the user who just connected obviously sends them self the message too. 当最终用户访问任何页面时,所有当前连接的用户都会收到一条消息“嗨,大家好!”,而刚刚连接的用户显然也向他们自己发送了该消息。 All as expected so far... 到目前为止一切都如预期...

The issue: I have a page that has a button on it... 问题:我的页面上有一个按钮...

<button id="register" type="button" class="btn btn-warning">Register</button>

When clicked, this should emit a message saying 'I want to register!' 单击后,这将发出一条消息,说“我要注册!” (currently to everyone but that doesn't matter yet). (当前适用于所有人,但这并不重要)。 In response, the server should listen for this and respond by emitting a message back saying 'Hmmm, will think about it!'. 作为响应,服务器应侦听此消息并通过发出一条回覆消息说“嗯,会考虑!”来做出响应。

'registering' is never heard at the server, and so cannot emit the reply of 'registration-response'. 服务器永远不会听到“正在注册”,因此无法发出“注册响应”的答复。

Can anyone see what I'm doing wrong? 谁能看到我在做什么错?

I've seen this SocketIO, can't send emit data from client and a couple others that are similar but I am already doing what they have suggested :/ 我见过这个SocketIO,无法从客户端和其他几个类似的对象发送发射数据,但是我已经在按照他们的建议进行操作了:/

NB : The app is running on http://localhost:3000/ and the io using http://localhost:8678/events . 注意 :该应用程序在http://localhost:3000/和io上使用http://localhost:8678/events If you need any further info just let me know 如果您需要更多信息,请告诉我

Client-side JS/jQuery (app.js) 客户端JS / jQuery(app.js)

var app = app || {};

app = (function($){

    $(function () {

        /////////////////////////////////////////////////////
        // Connect to io and setup listeners
        /////////////////////////////////////////////////////

        var socket = io.connect('http://localhost:8678/events');
        socket.on('connected', function (data) {
            console.log(data); // Always see this in the console
        });

        socket.on('registration-response', function (data) {
            console.log(data); // NOT seen in console
        });

        /////////////////////////////////////////////////////
        // Setup click events
        /////////////////////////////////////////////////////

        $('#register').on('click', function(){
            console.log('click'); // Always see this, so no binding issue
            socket.emit('registering', 'I want to register!');
        });
    });

})(jQuery);

Server-side JS 服务器端JS

var app = require('express');
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io').listen(server);  //pass a http.Server instance
server.listen(8678);  //listen on port 8678

////////////////////////////////////////
// Register event listeners     
////////////////////////////////////////

var events = io.of('/events');

events.on('connection', function(socket){ // This is heard and callback fires
  events.emit('connected', 'Hi everyone!'); // This gets sent fine
});

events.on('registering', function(data){
  events.emit('registration-response', 'Hmmm, will think about it!');
});

Your socket on the server isn't set up to listen for the event. 您服务器上的套接字未设置为监听事件。

You need to do it like this 你需要这样

events.on('connection', function(socket){ // This is heard and callback fires
    events.emit('connected', 'Hi everyone!'); // This gets sent fine
    socket.on('registering', function(data){
      events.emit('registration-response', 'Hmmm, will think about it!');
    });
});

The socket now knows what to do when it receives the 'registering' event and will send the response to the '/events' namespace. 套接字现在知道接收到“正在注册”事件时该怎么办,并将响应发送到“ /事件”命名空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM