刚安装了ws,试着库中的例子发现连接不了,也不报错,有没有人遇到过这个问题,怎么解决的 浏览器为chrome var WebSocketServer = require(’…/node_modules/ws’).Server , http = require(‘http’) , express = require(‘express’) , //app = express.createServer(); app = express();
app.use(express.static(__dirname + ‘/public’)); app.listen(8080);
var wss = new WebSocketServer({server: app}); wss.on(‘connection’, function(ws) { var id = setInterval(function() { ws.send(JSON.stringify(process.memoryUsage()), function() { /* ignore errors */ }); }, 100); console.log(‘started client interval’); ws.on(‘close’, function() { console.log(‘stopping client interval’); clearInterval(id); }); });
index.html
<!DOCTYPE html> <html> <head> <style> body { font-family: Tahoma, Geneva, sans-serif; } div {
display: inline;
}
</style>
</head>
<body>
Server StatsRSS:
Heap total:
Heap used:
<script> if ('WebSocket' in window) { alert("in window"); } function updateStats(memuse) { document.getElementById('rss').innerHTML = memuse.rss; document.getElementById('heapTotal').innerHTML = memuse.heapTotal; document.getElementById('heapUsed').innerHTML = memuse.heapUsed; }
var host = window.document.location.host.replace(/:.*/, '');
alert(host);
var ws = new WebSocket('ws://' + host + ':8080');
ws.onopen=function() {
alert("open");
};
ws.onmessage = function (event) {
alert("hello");
updateStats(JSON.parse(event.data));
};
</script>
</body>
</html>