问题描述
var express =请求('express')
,app = express()
,http = require('http')
,server = http.createServer(app)
,Twit = require('twit')
,io = require('socket.io')。listen(server);
server.listen(8080);
$ b $ //路由
app.get('/',function(req,res){
res.sendfile(__ dirname +'/index.html');
});
var watchList = ['love','hate'];
$ b io.sockets.on('connection',function(socket){
console.log('Connected');
var T = new Twit({
consumer_key:''
,consumer_secret:''
,access_token:''
,access_token_secret:''
})
T.stream('statuses / filter',{track:watchList},function(stream){
stream.on('tweet',function(tweet){
io.sockets.emit('stream',tweet.text);
console.log(tweet.text);
});
});
});
这是我的客户端
< script src =/ socket.io/socket.io.js\"> ;</script>
< script src =https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js>< / script>
< script>
$(function(){
var socket = io.connect('http:// localhost:8080');
socket.on('tweet',function(tweet){
$(
'< div class =tweet>'+ tweet.text +'< / div>');
});
});
< / script>
< / div>
当我运行节点app.js并尝试连接到localhost:8080时,我只是得到一个空白页,即使一切(soket.io,jquery,...)似乎都已正确加载。
以下是服务器输出示例:
info - socket .io开始
调试 - 提供静态内容/socket.io.js
调试 - 客户端授权
信息 - 握手授权pwH0dbx4WvBhzSQXihpu
调试 - 设置请求GET /socket.io/1 / websocket / pwH0dbx4WvBhzSQXihpu
debug - 为客户端设置心跳间隔pwH0dbx4WvBhzSQXihpu
debug - 客户端授权
debug - websocket写入1 ::
debug - websocket写入5 ::: {名称:stream,args:[RT @mintycreative:今天很棒聊天RT @SharonHolistic:治疗明天可用http://t.co/5Poq3KU08u现在预订#WestMidsHou ...]}
debug - websocket写入5 ::: {name:stream,args:[RT @ laurenpeikoff:#BREAKING @ScottsdalePD证实 - 警方正在调查Michael Beasley涉嫌性侵犯。@ 12News @ azcentr ...]}
希望你能帮我纠正我的错误。这里的代码没有任何错误:(服务器端)
var express = require('express')
,app = express()
,http = require('http')
,server = http.createServer(app)
,Twit = require('twit')
,io = require('socket.io')。
server.listen(8080);
$ b $ //路由
app.get('/',function(req,res){
res.sendfile(__ dirname +'/index.html');
});
var watchList = ['love','hate'];
var T = new Twit({
consumer_key:''
,consumer_secret:''
,access_token:''
,access_token_secret:''
})
io.sockets.on('connection',function(socket){
console.log('Connected');
var stream = T.stream('statuses / filter',{track:watchList})
stream.on('tweet',function(tweet){
io。 sockets.emit('stream',tweet.text);
});
});
});
(客户端)
< script src =/ socket.io/socket.io.js\"> ;</script>
< script src =https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js>< / script>
< script>
var socket = io.connect('http:// localhost:8080');
socket.on('stream',function(tweet){
$('#tweetd')。append(tweet +'< br>');
});
< / script>
< div id =tweetd>< / div>
< / div>
I'm trying to build a Twitter streaming web application using node.js socket.io and twit.
var express = require('express')
, app = express()
, http = require('http')
, server = http.createServer(app)
,Twit = require('twit')
, io = require('socket.io').listen(server);
server.listen(8080);
// routing
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
var watchList = ['love', 'hate'];
io.sockets.on('connection', function (socket) {
console.log('Connected');
var T = new Twit({
consumer_key: ''
, consumer_secret: ''
, access_token: ''
, access_token_secret: ''
})
T.stream('statuses/filter', { track: watchList },function (stream) {
stream.on('tweet', function (tweet) {
io.sockets.emit('stream',tweet.text);
console.log(tweet.text);
});
});
});
Here's my client side
<script src="/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
<script>
$(function(){
var socket = io.connect('http://localhost:8080');
socket.on('tweet', function(tweet) {
$(
'<div class="tweet">' + tweet.text + '</div>');
});
});
</script>
</div>
When I run node app.js and try to connect to localhost:8080 I just get a blank page, even if everything ( soket.io, jquery, ... ) seems to have loaded correctly.
Here's a sample of the server output :
info - socket.io started
debug - served static content /socket.io.js
debug - client authorized
info - handshake authorized pwH0dbx4WvBhzSQXihpu
debug - setting request GET /socket.io/1/websocket/pwH0dbx4WvBhzSQXihpu
debug - set heartbeat interval for client pwH0dbx4WvBhzSQXihpu
debug - client authorized for
debug - websocket writing 1::
debug - websocket writing 5:::{"name":"stream","args":["RT @mintycreative: Great to chat today RT @SharonHolistic: Treatments available tomorrow http://t.co/5Poq3KU08u Book yours now #WestMidsHou…"]}
debug - websocket writing 5:::{"name":"stream","args":["RT @laurenpeikoff: #BREAKING @ScottsdalePD confirms - police are investigating Michael Beasley for alleged sexual assault. @12News @azcentr…"]}
Hope you can help me to correct my mistakes.
Problem solved
Here's the code without any mistakes : (server side)
var express = require('express')
, app = express()
, http = require('http')
, server = http.createServer(app)
,Twit = require('twit')
, io = require('socket.io').listen(server);
server.listen(8080);
// routing
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
var watchList = ['love', 'hate'];
var T = new Twit({
consumer_key: ''
, consumer_secret: ''
, access_token: ''
, access_token_secret: ''
})
io.sockets.on('connection', function (socket) {
console.log('Connected');
var stream = T.stream('statuses/filter', { track: watchList })
stream.on('tweet', function (tweet) {
io.sockets.emit('stream',tweet.text);
});
});
});
(client-side)
<script src="/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
<script>
var socket = io.connect('http://localhost:8080');
socket.on('stream', function(tweet){
$('#tweetd').append(tweet+'<br>');
});
</script>
<div id="tweetd"></div>
</div>
这篇关于使用socket.io/node.js在网页上显示流媒体推文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!