简体   繁体   English

如何使用json数据从客户端发出套接字?

[英]How to use a json data for socket emit from client side?

What is the best method to use a mysql fetched data during socket emiting time? 在套接字发出时间期间使用mysql提取数据的最佳方法是什么? For example, I will have a data retrieved from database and now I would like to use that data on socket.emit('emit_name', function(data){ }) .What will be the best way? 例如,我将从数据库中检索数据,现在我想在socket.emit('emit_name', function(data){ })上使用该数据。最好的方法是什么?

I firstly, tested with easy way to find if it works or not rather then diving beginning into complicated one from json.I used two data as 'type' and 'fruit' something like this on "Client side" 首先,我测试了一种简单的方法来查找它是否有效,然后从json跳入了复杂的模型。我在“客户端”使用了两个数据作为“类型”和“水果”

var socket = io.connect( 'http://localhost:8080' );
var nameVal = "Type";
var msg = "apple";

socket.emit( 'messages', { type : nameVal, fruit: msg } );

on Server side 在服务器端

var socket = require( 'socket.io' );
var express = require( 'express' );
var http = require( 'http' );
var app = express();
var mysql = require('mysql');
var server = http.createServer( app );
var io = socket.listen( server );
var pool = mysql.createPool({
connectionLimit : 10,
host: "localhost",
user: "root",
password: "",
database: "mydb"
  }
);


io.sockets.on( 'connection', function() {

console.log( "User connected..!" );

io.client.on( 'messages', function(data) {
console.log( 'Message received ' + data.type + ":" + data.fruit );

  });

});

server.listen(8080);

After doing this all I just started my node server and I get "connected" on my socket on page load but my work on socket side isn't working at all as I wised.The result(problem) on socket part of server side gives this error as below image :- 完成所有这些操作后,我刚刚启动了节点服务器,并在页面加载时在套接字上“连接”,但我在套接字端的工作根本不起作用。我在服务器端的套接字部分的结果(问题)显示这个错误如下图:-

在此处输入图片说明

What might be the problem?How can I solve this issue? 可能是什么问题,我该如何解决?

This code is just wrong: 这段代码是错误的:

io.sockets.on( 'connection', function() {

console.log( "User connected..!" );

io.client.on( 'messages', function(data) {
console.log( 'Message received ' + data.type + ":" + data.fruit );

  });

});

It should be this: 应该是这样的:

io.on( 'connection', function(socket) {

    console.log( "User connected..!" );

    socket.on( 'messages', function(data) {
        console.log( 'Message received ' + data.type + ":" + data.fruit );
    });
});

You have to use the socket variable that is passed to the io.on('connection , ...)` event to set event handlers on the newly connected socket. 您必须使用传递给io.on('connection ,...)`事件的socket变量在新连接的套接字上设置事件处理程序。


In addition, this line of your client code: 此外,您的客户代码的这一行:

socket.emit( 'messages', { type : nameVal, fruit: msg } );

may be running too soon before the socket has finished connecting. 可能在套接字完成连接之前运行太早了。 io.connect() is asynchronous. io.connect()是异步的。 It does not complete immediately. 它不会立即完成。 If you try to send data before it is connected, then there is not yet a live socket to actually send the data on. 如果尝试在连接之前发送数据,则还没有活动的套接字可以实际发送数据。 It's possible that socket.io will cache that data until the socket connects (you'd have to check the socket.io code to see for sure), but it is much safer to wait for the connect event on the socket before sending data. socket.io可能会缓存该数据,直到套接字连接为止(您必须检查socket.io代码以确保确定),但是在发送数据之前等待套接字上的connect事件要安全得多。

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

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