2014-10-25 3 views
1

헬륨 소켓 테스트를 원하는 모든 신체가 연결될 때 경고가 나타나야합니다. 노드 js가 설치되어 있지만 다른 컴퓨터에는 설치되지 않은 컴퓨터에서 작동합니다.연결이 거부 됨 노드 js

오류 :

Failed to load resource: net::ERR_CONNECTION_REFUSED http://xxx.xxx.xxx.xxx:8080/socket.io/socket.io.js Uncaught ReferenceError: io is not defined

코드 : 서버 :

var http = require('http'); 
var fs = require('fs'); 
var io = require('socket.io'); 

var server = http.createServer(function(req,res){ 
    res.writeHead(200,{'Content-Type' : 'text/html'}); 
    fs.readFile('./index.html',function(err,content){ 
     res.end(content); 
    }); 

}).listen(8080); 

io = io.listen(server) 

io.sockets.on('connection',function(client){ 

client.emit('connecte'); 

}); 

클라이언트 :

<html> 
<meta charset='utf-8'/> 
<head> 
    <title>my first page</title> 
<script src="http://localhost:8080/socket.io/socket.io.js" ></script> 
</head> 
<body> 
    <h1>It work</h1> 
</body> 
<script> 
var socket; 

socket = io.connect('http://localhost:8080'); 

socket.on('connecte', function(){ 
     alert('You are connected'); 
    }); 

</script> 
</html> 

영어를 유감스럽게 생각하며 영어를 배우려하지 않습니다. 감사합니다

답변

1

Socket.IO docs은 내장 된 http 서버와 함께 Socket.IO를 사용하는 방법을 보여줍니다. 이 예제와 코드를 비교하면 io이 옳지 않다는 것을 알 수 있습니다. 이 같은 시도 : 또한

var http = require('http'); 
var fs = require('fs'); 
var io = require('socket.io'); 

var server = http.createServer(function(req, res){ 
    res.writeHead(200, {'Content-Type': 'text/html'}); 
    fs.readFile('./index.html', function(err, content) { 
    res.end(content); 
    }); 
}); 

io = io(server); 

server.listen(8080); 

io.on('connection', function(client) { 
    client.emit('connected'); 
}); 

관련이없는 메모를 대신 먼저 전체 파일을 버퍼링의 클라이언트에 html 파일을 pipe() 수 :

res.writeHead(200, {'Content-Type': 'text/html'}); 
    fs.createReadStream('index.html').pipe(res); 
관련 문제