노드 JS와 소켓 IO를 실험하고 있습니다. Socket.IO 안내서 시작하기 : https://socket.io/get-started/chat/.웹 페이지에 TCP 소켓 메시지를 출력하기위한 소켓 IO
하는 index.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
에 대한 동일한 원리에 index.html을
<!doctype html>
<html>
<head>
<title>Chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function() {
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
});
</script>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
</body>
</html>
에 나는에 partecipants 중 하나를 교체하려고 : 여기 코드입니다 컴퓨터로 채팅하십시오. 그래서 포트 3000에 연결하고 자체 텍스트 메시지 (단순한 문자열)를 보내는 C++ 코드를 작성했습니다.
C++ 쪽에서는 소켓 열기, 연결, 소켓 쓰기에 아무런 문제가 없었습니다. NODE JS에서 서버는 포트에서 들어오는 연결을 수신하지만 클라이언트 연결을 감지하지 못합니다.
NODE 서버로 메시지를 받고 웹 페이지에 출력하기 위해 어떻게 보낼 수 있습니까?
사이드 노트 : 내 C++에서 원시 프로토콜 인 TCP 프로토콜을 사용하고있는 것으로 나타났습니다. Socket.IO는 HTTP를 기반으로 구축되었으므로 대신 HTTP를 사용해야한다고 생각합니다. 이 경우 POST metod로 HTTP 요청을 만들어야한다고 생각합니다. 맞습니까?