socket.io客户端收到错误的数据

时间:2018-08-05 17:00:05

标签: javascript php wordpress socket.io zeromq

我有一个实时项目,由于某种原因,我的socket.io客户端接收到了错误地从服务器发出的数据。我在控制台登录时并不一定会收到错误,它似乎得到了某些东西而不是预期的东西。这就是我收到的:

客户端控制台: enter image description here

这很新,所以我不知道该日志的含义。为了解释我的代码,它是php和zmq的混合物,用于将数据从wordpress传输到javascript,本质上是实时更新帖子。当我记录数据时,socket.io服务器从php端正确接收了数据,当从套接字服务器转到套接字客户端时,似乎在翻译中丢失了一些东西。

socket.io服务器:

const express = require('express');
const socketIO = require('socket.io');
const http = require('http');
const zmq = require('zmq'),
  zSock = zmq.socket('pull');
  zSock.bind('tcp://127.0.0.1:5556');


// app set up
const app = express();
const server = http.Server(app);

let port = process.env.PORT || 8080;

// static files
app.use(express.static('app'));

// socket setup & pass SERVER
const io = new socketIO(server);

// on client connect
io.on('connection', (socket) => {

  console.log('client has entered...');

//when data is received from php/zmq, emit data
  zSock.on('message', (data) => {

    console.log('zmq data: ', data);
    socket.emit('test', data);

  });

});

socket.io客户端:

import $ from 'jquery';
import io from 'socket.io-client';

  const socket = io.connect('http://localhost:8080/');

  class ShowTeam {

    constructor(){
      this.teamOutput = $('.team-random__output');
      this.events();

    }

    events() {
      socket.on('test', (data) => {

        console.log('connected!');

        console.dir(data);
        console.log('client data: ' + data);

      });
    }

  }

export default ShowTeam;

这是socket.io服务器接收到的正确数据,应该发送给客户端。它应该是删除了一些引号的html字符串,因此可以通过json正确传输:

zmq data:  {"updated_post_content":"<div class= team-container team-container--inline col col--md-2 col--lg-2 col--xl-2><img src=/card-store/wp-content/themes/card-store-theme/images/baseball/team0.jpg> <p>Brianne<p/></div><div class= team-container team-container--inline col col--md-2 col--lg-2 col--xl-2><img src=/card-store/wp-content/themes/card-store-theme/images/baseball/team1.jpg> <p>Brianne<p/></div><div class= team-container team-container--inline col col--md-2 col--lg-2 col--xl-2><img src=/card-store/wp-content/themes/card-store-theme/images/baseball/team2.jpg> <p>Brianne<p/></div><div class= team-container team-container--inline col col--md-2 col--lg-2 col--xl-2><img src=/card-store/wp-content/themes/card-store-theme/images/baseball/team3.jpg> <p>Brianne<p/></div><div class= team-container team-container--inline col col--md-2 col--lg-2 col--xl-2><img src=/card-store/wp-content/themes/card-store-theme/images/baseball/team4.jpg> <p>Brianne<p/></div>"}

1 个答案:

答案 0 :(得分:0)

从zmq套接字接收到的对象是缓冲区,即数据的序列化。要从中获取对象,您必须执行const msg = JSON.parse(data.toString());