我想到了.只是不得不搬家
socket.on('connect', function () { port.postMessage('connect'); }); socket.on('disconnect', function () { port.postMessage('disconnect'); });
进入self.addEventListener("connect", function(e) {});
中worker.js
和改变var socket=io.connect('http://38.98.xxx.xxx:6000');
到
var socket = io('http://38.98.xxx.xxx:6000');
以下是工作示例,如果有人需要的话.
worker.js
importScripts('socket.io.js'); var socket = io('http://38.98.xxx.xxx:6000'); var connections = 0; self.addEventListener("connect", function(e) { var port = e.ports[0]; connections ++; port.addEventListener("message", function(e) { if (e.data === "start") { port.postMessage('hello'); } }, false); port.start(); socket.on('push', function(pushed){ port.postMessage(pushed); }); socket.on('connect', function () { port.postMessage('connect'); }); socket.on('disconnect', function () { port.postMessage('disconnect'); }); }, false);