2014-11-28 5 views
5

서버에 websocket 및 socket.io가 설치되어 있습니다. 내가 브라우저 페이지를로드 할 때, 나는 콘솔에서이 오류가 발생합니다 : catch되지 않은 형식 오류 : 정의되지 않은 함수 아니다 (socket.io-1.2.1.js : 1) 여기서 socket.io error undefined is not function

서버 측 코드는 다음과 같습니다

// Require HTTP module (to start server) and Socket.IO 
var http = require('http'), io = require('socket.io'); 

// Start the server at port 9602 
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(9602); 

// 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'); 
    }); 

}); 

그리고 클라이언트 측 코드 : 어떤 도움에 감사드립니다

<script src="https://cdn.socket.io/socket.io-1.2.1.js"></script> 

<script>  
    // Create SocketIO instance, connect 
     var socket = new io.Socket('localhost',{ 
     port: 9602 
     }); 
     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); 
     } 

    </script> 

. k4elo

+0

CDN 다음하지 않는다 작동 승 모자는 clearInterval (간격)입니다. 에 대한? – adeneo

+0

이 확실하다; –

+0

오류가 더 도움이되도록 라이브러리의 비 압축 버전을 사용해 보는 것이 도움이 될 수 있습니다. – Pointy

답변

0

당신은 HTTP에 서버를 작성하지 HTTPS 대신 스크립트 SRC의

<script src='/socket.io/socket.io.js'></script> 

= "https://cdn.socket.io/socket.io-1.2.1.js">

`요즘, 당신에게`io.Socket()`기능
4
var socket = io("http://127.0.0.1:9000"); 

// Add a connect listener 
socket.on('connect',function() { 
    console.log('Client has connected to the server!'); 
}); 

위의 방법) 그것은 단지`VAR 소켓 = 10합니다 (

관련 문제