0
VPS에서 node.js/socket.io를 사용하고 있습니다. 기본 전송을 시도합니다. &을 수신합니다. 괜찮아요. 모든 것을 승인하는 것 같아요. VPS에서 이걸 얻습니다. : websocket writing 5:::{"name":"news","args":[{"hello":"world"}]}
클라이언트가 연결을 끊었을 때만 볼 수 있습니다. 연결이 끊어지면 "세계"라는 단어가 표시됩니다. 나는 그들이 틀림없이 연결되어있는 동안 그 단어를 보지 말아야하는 것처럼 잘못된 것을하고 있습니까? 도와 주셔서 감사합니다.연결이 끊어진 경우에만 결과가 표시됩니다.
이 코드를 사용하고 있습니다 :
클라이언트 :
<!doctype html>
<html>
<head>
<title>web sockets</title>
<meta charset="utf-8">
<script src="http://IP-ADDRESS/socket.io/socket.io.js"></script>
<script>
var socket = io
.connect('http://IP-ADDRESS:80');
socket.on('news', function (data) {
console.log(data);
writeMessage(data);
socket.emit('my other event', { my: 'data' });
});
function writeMessage(msg) {
var msgArea = document.getElementById("msgArea");
if (typeof msg == "object") {
msgArea.innerHTML = msg.hello;
}
else {
msgArea.innerHTML = msg;
}
}
</script>
</head>
<body>
<div id="msgArea">
</div>
</body>
</html>
서버 :
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(80);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});