想做推送的功能,看到下面这个简易聊天室,运行了下,在低版本ie下用不了。请问如何解决呢?* 前台代码:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
$(function(){
// var iosocket = io.connect();
if (/Firefox\/\s/.test(navigator.userAgent)){
var iosocket = io.connect('localhost:5000',{transports:['xhr-polling']});
}
else if (/MSIE (\d+.\d+);/.test(navigator.userAgent)){
var iosocket = io.connect('localhost:5000',{transports:['jsonp-polling']});
}
else {
var iosocket = io.connect('localhost:5000');
}
iosocket.on('connect', function () {
$('#incomingChatMessages').append($('<li>Connected</li>'));
iosocket.on('message', function(message) {
$('#incomingChatMessages').append($('<li></li>').text(message));
});
iosocket.on('disconnect', function() {
$('#incomingChatMessages').append('<li>Disconnected</li>');
});
});
$('#outgoingChatMessage').keypress(function(event) {
if(event.which == 13) {
event.preventDefault();
iosocket.send($('#outgoingChatMessage').val());
$('#incomingChatMessages').append($('<li></li>').text($('#outgoingChatMessage').val()));
$('#outgoingChatMessage').val('');
}
});
});
</script>
</head>
<body>
Incoming Chat: <ul id="incomingChatMessages"></ul>
<br />
<input type="text" id="outgoingChatMessage">
</body>
</html>
服务器代码:
var fs = require('fs'),
http = require('http'),
socketio = require('socket.io');
var server = http.createServer(function(req, res) {
res.writeHead(200, { 'Content-type': 'text/html'});
res.end(fs.readFileSync(__dirname + '/index.html'));
}).listen(5000, function() {
console.log('Listening at: http://localhost:5000');
});
socketio.listen(server).on('connection', function (socket) {
socket.on('message', function (msg) {
console.log('Message Received: ', msg);
socket.broadcast.emit('message', msg);
});
});
7 回复
socket.io需要配置不同的协议已支持不同版本的浏览器,理论上从IE6开始就能支持
var io = require('socket.io').listen(server);
require('./config/sio')(io);
config/sio.js中如下配置: sio.configure(function() { //sio.enable(‘browser client minification’); // send minified client //sio.enable(‘browser client etag’); // apply etag caching logic based on version number //sio.enable(‘browser client gzip’); // gzip the file sio.set('log level’, 1); // reduce logging
// enable all transports (optional if you want flashsocket support, please note that some hosting
// providers do not allow you to create servers that listen on a port different than 80 or their
// default port)
sio.set('transports', ['websocket', 'flashsocket', 'htmlfile', 'xhr-polling', 'jsonp-polling']);
//sio.set('store', store);
sio.set('store', new RedisStore({
redisPub : pub
, redisSub : sub
, redisClient : client
}));
sio.set('authorization', function(data, accept) {
// check if there's a cookie header
var cookies = data.headers.cookie;
if (cookies) {
cookies = cookie.parse(cookies);
//cookies = utils.parseSignedCookies(cookies, 'dfdfdfdfgegsgkkafkjgoijgmv');
cookies = utils.parseJSONCookies(cookies);
data.sessionID = cookies['express.sid'];
} else {
// if there isn't, turn down the connection with a message
// and leave the function.
return accept('No cookie transmitted.', false);
}
// accept the incoming connection
accept(null, true);
});
});