홈 네트워크에 Ubuntu 10.04 서버를 설치했습니다. Lamp-server, NodeJs 및 Socket.io를 설치했습니다. 그게 잘 작동하는 것 같지만 내 클라이언트에서 서버로 연결할 수 없습니다. 클라이언트 파일을 저장할 위치 및/또는 IP 주소 및 포트를 코드에 저장하는 방법에 대한 요점을 놓치고 있다고 생각합니다.NodeJS + Socket.io : 클라이언트가 홈 네트워크의 Ubuntu 서버에 연결하지 않습니다.
David Walsh (http://davidwalsh.name/websocket)의 예를 사용하고 있습니다. 내 IP 주소는 192.168.1.113입니다. 클라이언트에 나는 노드가 아로 설치되어있는 곳입니다
/usr/지방/빈
에을 app.js 저장합니다.
서버 코드 내가 노드로 서버를 시작하고 브라우저 (http://192.168.1.113:8000) 내가 볼에 ADRESS를 입력 '안녕하세요 소켓 연인!'
// Require HTTP module (to start server) and Socket.IO
var http = require('http'), io = require('socket.io');
// Start the server at port 8000
var server = http.createServer(function(req, res){
// Send HTML headers and message
res.writeHead(200,{ 'Content-Type': 'text/html' });
res.end('<h1>Hello Socket Lover!</h1>');
});
server.listen(8000);
// Create a Socket.IO instance, passing it our server
var socket = io.listen(server);
// Add a connect listener
socket.on('connection', function(client){
// Success! Now listen to messages to be received
client.on('message',function(event){
console.log('Received message from client!',event);
});
client.on('disconnect',function(){
clearInterval(interval);
console.log('Server has disconnected');
});
});
그래서 나는 이것이 잘 작동한다고 생각한다.
나는
의/var/www가 내 LAMP 서버의 홈 폴더가있는 곳입니다
내 클라이언트 파일 (client.html)를 저장합니다. 내가 브라우저에서이 파일을 열 때 Client.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script src="http://cdn.socket.io/stable/socket.io.js"></script>
<script>
// Create SocketIO instance, connect
var socket = new io.Socket('http://192.168.1.113',{
port: 8000
});
socket.connect();
// Add a connect listener
socket.on('connect',function() {
console.log('Client has connected to the server!');
});
// Add a connect listener
socket.on('message',function(data) {
console.log('Received a message from the server!',data);
});
// Add a disconnect listener
socket.on('disconnect',function() {
console.log('The client has disconnected!');
});
// Sends a message to the server via sockets
function sendMessageToServer(message) {
socket.send(message);
}
</body>
</html>
지금 (http://192.168.1.113/client.html) 아무 반응이 없습니다. 내 우분투 서버 또는 다른 로그에 연결이 보이지 않으며 브라우저에 메시지가 없습니다.
음 ... 내가 뭘 잘못하고 있니? 나는 몇 시간 동안 노력했지만 모든 종류의 것들을 바꿨지 만 여전히 결과는 없습니다.
당신이 그것을 연결을 할 수 있도록 여러분의 우분투 서버에서 포트 8000을 열었습니다? – pmavik
도움 주셔서 감사합니다. 나는 ufw를 사용하여 개폐를 시도했다.포트를 닫으면 'Hello Socket Lovers'메시지가 표시되지 않습니다. 다시 열면 메시지가 표시되어 포트 문제라고 생각하지 않습니다. –
질문에서 태그가 client.html 파일에 없습니다. 나는 그것도 추가했지만 결과가 없습니다. –