저는 HTML5 및 node.js를 처음 사용합니다. 매우 기본적인 클라이언트 - 서버 응용 프로그램을 만들려고합니다. 여기에 코드가 있습니다.websockets 및 node.js를 사용하는 웹 응용 프로그램
서버 측 (Node.js를)
var net = require('net');
var server = net.createServer(function(c) {
console.log('client connected');
c.setEncoding('utf8');
c.on('end', function() {
console.log('client disconnected');
});
c.on('data', function(data) {
console.log(data);
c.write("Got it");
});
});
server.listen(9998);
클라이언트 측 (WebSocket을) :
<!DOCTYPE html>
<html>
<head>
<script>
try {
var ws = new WebSocket('ws://127.0.0.1:9998');
ws.onopen = function() {
ws.send("Message to send");
alert("Message is sent...");
};
ws.onmessage = function (evt) {
var message = evt.data;
alert("Message is received: " + message);
};
ws.onclose = function() {
alert("Connection is closed...");
};
} catch (err) {
alert(err.message);
}
</script>
</head>
<body>
</body>
</html>
지금까지 내가 이해
는, 클라이언트가 서버에 연결 보내야합니다 "메시지 보내기 "하고 서버는"알았습니다 "로 응답해야합니다. 대신 서버가받는 것은 클라이언트 HTML 페이지에 대한 http GET 요청이며 클라이언트 콜백은 실행되지 않습니다. 내가 뭘 놓치고 있니?
감사합니다. –