2016-05-14 105 views
0

我正在使用d3创建数据vizsualisation项目的个人项目。我想从服务器传输数据到客户端,在那里我将可视化它。无法连接客户端和服务器import.io

想法是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> 
+0

你肯定'/socket.io/socket.io。 js'存在于你的服务器上,它有适当的权利?检查文件是否正在您的浏览器网络检查器选项卡中正确下载。 – dlopez

+0

对于哑巴问题的赦免 - 如何正确下载? npm安装在服务器文件夹内,还有什么? – andris

+0

@andris您正在使用客户端库混合服务器的节点模块,请参阅下面我编辑的答案。 – risuch

回答

1

的socket.io客户端的lib可以通过CDN直接加载这样的:

<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script> 

但是如果你想在你的客户端独立于互联网,你需要从这里获取客户端lib,然后将其放置在服务器上的某个文件夹中,然后通过use()方法公开访问,以便客户端html可以得到文件,添加如下内容到你的应用程序:

var express = require('express'); 
app.use(express.static(__dirname + '/public')); 

其中公共是目录中的socket.io LIB在于其中