页面刷新时的socket.io增加了请求

时间:2018-08-29 10:43:26

标签: websocket socket.io

我使用Elephant.io插件进行php集成。我是socket.io的新手,也不知道我哪里错了, 代码工作正常,但唯一的问题是,当我执行任何操作时,我会从每个操作中获得9个请求。 server.js

var socket  = require('socket.io'),
    express = require('express'),
    https   = require('https'),
    http    = require('http');

console.log('SocketIO > listening on port ');

var app=express();

    var http_server = http.createServer(app).listen(4001);



    function emitNewOrder(http_server){
      var io = socket.listen(http_server);

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


      socket.on("updateorder", function(data){



        io.emit("updateorders", data);
    });


        socket.on('disconnect', function () {

          console.log('Socket.io disconnect');

        }); 

    });

}
emitNewOrder(http_server);

客户端

if($results){
    $version = new Version2X("http://localhost:4001");

                $client = new Client($version);

                $client -> initialize();

                $client -> emit("updateorder", $results);
             // $client -> emit("showrows",$form_data);
                $client -> close();

        }

var socket = io.connect("http://localhost:4001");

            socket.on("updateorders", function(data) {
                //console.log(data);
             // encodeURIComponent(JSON.stringify(data));
)};

0 个答案:

没有答案