2014-11-28 14 views
5

Zainstalowany websocket i socket.io na serwerze. Kiedy załadować stronę w przeglądarce, otrzymuję ten błąd w konsoli: Uncaught TypeError: niezdefiniowane nie jest funkcją (socket.io-1.2.1.js: 1)nieokreślony błąd socket.io nie jest funkcją

Oto kod po stronie serwera:

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

}); 

a klient kod boczny:

<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> 

Każda pomoc jest mile widziana. k4elo

+0

Czy to nie jest po prostu 'var socket = IO();' te dni, jesteś pewien, że to 'io.Socket()' funkcja – adeneo

+0

W hat is clearInterval (interval); dla? –

+0

To może bardzo pomóc w wypróbowaniu nieminurowanej wersji biblioteki, aby błąd był bardziej przydatny. – Pointy

Odpowiedz

0

Ty tworzysz serwer HTTP na HTTPS nie

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

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

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

Powyższa metoda działa z następującymi cdn