我在从本地rethinkdb服务器获取套接字以将数据流传输到客户端时遇到问题。我对sockets.io相当新,所以我不确定我做错了什么。有人可以看看我的代码并帮助我吗?感谢
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var r = require('rethinkdb');
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
r.connect({ db: "test" }).then(function(conn) {
r.table('Articles').getField('title').run(conn)
.then(function(cursor) {
cursor.each(function(err, item) {
console.log(item);
io.sockets.emit("title", item);
})
})
})
http.listen(3000, function(){
console.log('listening on *:3000');
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Socket.IO chat</title>
<style media="screen">
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
</style>
</head>
<body>
<div id="content">
</div>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io('http://localhost:3000');
socket.on("title", function(data) {
console.log(data);
$('#content').append($('<p>').text(data));
});
</script>
</body>
</html>