最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - socket.io error undefined is not a function - Stack Overflow

programmeradmin3浏览0评论

Installed websocket and socket.io on the server. When I load the browser page, I get this error in the console: Uncaught TypeError: undefined is not a function (socket.io-1.2.1.js:1)

Here is the server side code:

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

});

And the client side code:

<script src=".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>

Any help is appreciated. k4elo

Installed websocket and socket.io on the server. When I load the browser page, I get this error in the console: Uncaught TypeError: undefined is not a function (socket.io-1.2.1.js:1)

Here is the server side code:

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

});

And the client side code:

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

Any help is appreciated. k4elo

Share Improve this question asked Nov 28, 2014 at 16:56 K4eloK4elo 731 silver badge5 bronze badges 5
  • Isn't it just var socket = io(); these days, are you sure there is a io.Socket() function – adeneo Commented Nov 28, 2014 at 17:01
  • What is clearInterval(interval); for? – Sergey Yarotskiy Commented Nov 28, 2014 at 17:02
  • It might help a lot to try the non-minified version of the library so that the error is more helpful. – Pointy Commented Nov 28, 2014 at 17:06
  • Just trying sample code from socket.io to see if I can get a connection. <script src='/socket.io/socket.io.js'></script> didn't work-not found. This: <script src="cdn.socket.io/socket.io-1.2.1.js"></script> finds the file but still give the "undefined is not a function" error from line 1 of socket.io-1.2.1.js file. – K4elo Commented Nov 28, 2014 at 18:10
  • If anyone can point me to a server and client example for socket.io that works, I would be very grateful. – K4elo Commented Nov 28, 2014 at 18:38
Add a ment  | 

2 Answers 2

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

The above method works with the following cdn

You are creating server on HTTP not HTTPS

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

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

发布评论

评论列表(0)

  1. 暂无评论