可以't连接客户端和服务器import.io
can't connect client and server import.io
我正在进行一个个人项目,用d3创建数据可视化项目。我想把数据从服务器流式传输到客户端,在那里我可以看到它
想法是node.js+socket.io+d3.js
我有我的数据流,但现在我正在使用socket.io将服务器数据流测试到客户端,但我经常失败。所以我用下面提到的代码迈出了小步,但也失败了。(代码btw来自官方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库所在的目录
相关文章:
- 使用JSP从服务器检索和显示图像
- 如何使用skip参数使用angular ui引导进行服务器端分页
- 客户端服务器REST API captcha实现
- 正在将base64 jpeg从input-type=file上传到服务器
- Webpack开发服务器和React服务器端渲染
- 提示使用服务器端事件处理程序激活JavaScript
- 使用谷歌应用程序脚本将服务器端数据表返回到客户端
- 如何使用Socket.io将命令从客户端发送到服务器
- jQuery blueimp文件上传:将N-1个文件上传到IE中的服务器
- 使用jasmine模拟对服务器的调用
- 如何轻松地将服务器端变量从Java代码转移到客户端代码
- 从客户端获取修改后的对象,并将其与服务器上的原始对象组合
- 通过ajax将坐标传递到php服务器端,并在处理后检索到javascript
- 如何使用Javascript将空数组发送到PHP服务器
- 如何在HTML中显示服务器对象变量
- 如何配置分析以将数据发送到我自己的服务器
- 我无法使用angularJs($http)访问服务器
- 如何检测第三方广告服务器请求
- 可以't连接客户端和服务器import.io
- Webpack require/import在客户端而不是服务器上懒散地运行