WebSocket服务器无法立即接收消息

时间:2015-06-16 00:35:03

标签: javascript node.js heroku websocket

Node.JS服务器没有快速收到消息..

服务器代码:

var WebSocketServer = require("ws").Server
var http = require("http")
var express = require("express")
var app = express()
var port = process.env.PORT || 5000

app.use(express.static(__dirname + "/"))

var server = http.createServer(app)
server.listen(port)

console.log("http server listening on %d", port)

var wss = new WebSocketServer({server: server})
console.log("websocket server created")

wss.on("connection", function(ws) {
  var id = setInterval(function() {
    ws.send(JSON.stringify(new Date()), function() {  })
  }, 1000)

  console.log("websocket connection open")
  ws.on("message", function(msg){
    ws.send(msg);
  })
  ws.on("close", function() {
    console.log("websocket connection close")
    clearInterval(id)
  })
})

连接代码:

var z = new WebSocket("ws://appname.herokuapp.com/");
z.onopen = function(){ console.log("opened"); };
// ... after opened message seen in console, the following
// were typed directly into the console, a few seconds apart
z.send("H");
z.send("Hello World");
z.send("Hello World asdf");

在第三次发送完成之前(即使它在其他发送后一分钟),服务器也没有回复HHello World;所有三个回复都是在同一时间做出的。如何让服务器及时响应?

(使用WireShark,我检查了发送实际已完成;它们是)

编辑:每次响应之前的请求/时间量。

0 个答案:

没有答案