아파치와 Socket.IO가있는 실행중인 서버가 있습니다. 내 웹 사이트에서 socket.io를 사용하여 메시지를 보내고 받으려고합니다. Socket.IO Websocket Firefox 및 Chrome에서 작동하지 않는 메시지 보내기
이
내 서버의 코드입니다 :var fs = require('fs');
var hskey = fs.readFileSync('file.key');
var hscert = fs.readFileSync('file.crt');
var options = {
key: hskey,
cert: hscert
};
var app = require('https').createServer(options);
var io = require('/usr/local/lib/node_modules/socket.io').listen(app);
app.listen(8181);
io.sockets.on('connection', function (socket) {
socket.emit('serverMessage', 'Bienvenue master!');
socket.broadcast.emit('serverMessage', 'New user online');
});
그리고 이것은 웹 페이지입니다 :
<!doctype html>
<html>
<head>
<title>Socket.io Test</title>
<script src="./socket.io.js"></script>
</head>
<body>
<script>
var socket;
var firstconnect = true;
function connect() {
if(firstconnect) {
socket = io.connect('https://secure.mysite.com:8181');
socket.on('serverMessage', function(data){ message(data); });
socket.on('connect', function(){ status_update("Connected to Server"); });
socket.on('disconnect', function(){ status_update("Disconnected from Server"); });
socket.on('reconnect', function(){ status_update("Reconnected to Server"); });
socket.on('reconnecting', function(nextRetry){ status_update("Reconnecting in "
+ nextRetry + " seconds"); });
socket.on('reconnect_failed', function(){ message("Reconnect Failed"); });
firstconnect = false;
}
else {
socket.socket.reconnect();
}
}
function disconnect() {
socket.disconnect();
}
function message(data) {
document.getElementById('message').innerHTML += "<br>" + "Server says: " + data;
}
function status_update(txt){
document.getElementById('status').innerHTML = txt;
}
function esc(msg){
return msg.replace(/</g, '<').replace(/>/g, '>');
}
function send() {
socket.send('clientMessage', 'world');
};
</script>
<h1>Socket.io Test</h1>
<div><p id="status">Waiting for input</p></div>
<div><p id="message"></p></div>
<button id="connect" onClick='connect()'/>Connect</button>
<button id="disconnect" onClick='disconnect()'>Disconnect</button>
<button id="send" onClick='send()'/>Send Message</button>
</body>
</html>
모든 사파리 (웹 소켓)와 오페라 (JSON 풀링)에서 잘 작동하는 것 같다 만에 Firefox 및 Chrome (websocket) 클라이언트에서 서버로 메시지를 보낼 수 없습니다. 다른 모든 작업, 핸드 셰이크, 연결할 수 및 서버 메시지를 가져옵니다. 나는 연구의 할당을 만들었지 만 나는이 문제가있는 유일한 사람처럼 보입니다.
도와 주셔서 감사합니다.
웹 소켓의 경우 "브라우저 호환성"을 보셨습니까? https://developer.mozilla.org/en/WebSockets –
크롬에도 표준 웹킷과는 다른 웹 소켓 구현이있을 수 있습니다. –
websocket 용으로 socket.io를 사용했기 때문에 flashsocket과 long pooling도 가능하므로 작동하지만 메시지를 보내지 마십시오. – exomic