我正在进行一个个人项目,使用 d3 创建数据可视化项目。我想将数据从服务器流式传输到客户端,然后将其可视化。
想法是node.js + socket.io + d3.js
我有我的数据流,但现在我使用 socket.io 测试到客户端的服务器数据流,但我经常失败。所以我用下面提到的代码采取了一些小步骤,但这也失败了。 (顺便说一句,代码来自官方socket.io页面)
一直提示找不到资源文件:
Failed to load resource: The requested URL was not found on this server.
ReferenceError: Can't find variable: io
我正在研究很多其他教程和示例,现在我完全困惑了。有人可以帮忙吗?也许有其他建议而不是socket.io
服务器:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendfile('index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
});
http.listen(3001, function(){
console.log('listening on *:3000');
});
客户端
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io();
</script>
</body>
</html>
最佳答案
socket.io 客户端库可以通过 CDN 直接加载,如下所示:
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
但是如果你想在你的客户端中独立于互联网,你需要从这里获取客户端库 http://socket.io/download/ ,然后将其放入服务器上的某个文件夹中,然后通过 use() 使其可公开访问,以便客户端 html 可以获取该文件,将如下内容添加到您的应用程序中:
var express = require('express');
app.use(express.static(__dirname + '/public'));
其中public是socket.io库所在的目录
关于javascript - 无法连接客户端和服务器 import.io,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37228636/