我似乎已经设置了 socket.io 并试图发出一些推文,但它们没有出现在浏览器中。这就是我在控制台中得到的:
GET / 304 16ms
debug - served static content /socket.io.js
debug - client authorized
info - handshake authorized LYlpc7dcpLyHCFcKYUJy
debug - setting request GET /socket.io/1/websocket/LYlpc7dcpLyHCFcKYUJy
debug - set heartbeat interval for client LYlpc7dcpLyHCFcKYUJy
debug - client authorized for
debug - websocket writing 1::
debug - websocket writing 5:::{"name":"tweets","args":[{"user":"AaronNewport97","text":"You're not your","latitude":51.56764399,"longitude":0.49722754}]}
51.56764399 0.49722754
以下是 app.js 中的相关代码:
if(data.geo!=null){
latitude = data.geo.coordinates[0];
longitude = data.geo.coordinates[1];
io.sockets.volatile.emit('tweets', {
user: data.user.screen_name,
text: data.text,
latitude: latitude,
longitude: longitude
});
}
这就是我在 html 方面得到的:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
jQuery(function ($) {
var tweetList = $('ul.tweets');
socket.on('tweet', function (data) {
tweetList .prepend('<li>' + data.user + ': ' + data.text + '</li>');
});
});
</script>
在浏览器控制台中,我有:
Request URL:ws://localhost:3000/socket.io/1/websocket/vBFWuusMIQbO9PeJZv8p
Request Method:GET
Status Code:101 Switching Protocols
看起来连接正在等待。为什么要这样做?