HTML5 と 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);
クライアント側 (ウェブソケット):
<!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 要求であり、クライアントのコールバックはまったく発生しません。私は何が欠けていますか?