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

javascript - TypeError: io.connect is not a function - Stack Overflow

programmeradmin5浏览0评论

I am following a tutorial for a simple multiplayer game using node js and socket.io (.html) . I am having a problem when i am trying to connect to the socket server.I get an error saying TypeError: io.connect is not a function PS : i am a total beginner in node js and socket.io so please help me out.

var util = require("util");
io = require("socket.io"),
    Player = require("./Player").Player;
var socket, players;
function init(){
    players = [];

    socket = io.listen(8000);

    socket.configure(function() {
        socket.set("transports", ["websocket"]);
        socket.set("log level", 2);
    });

    setEventHandlers();

    socket = io.connect("http://localhost", {port: 8000, transports: ["websocket"]});

};

I am following a tutorial for a simple multiplayer game using node js and socket.io (http://rawkes./articles/creating-a-real-time-multiplayer-game-with-websockets-and-node.html) . I am having a problem when i am trying to connect to the socket server.I get an error saying TypeError: io.connect is not a function PS : i am a total beginner in node js and socket.io so please help me out.

var util = require("util");
io = require("socket.io"),
    Player = require("./Player").Player;
var socket, players;
function init(){
    players = [];

    socket = io.listen(8000);

    socket.configure(function() {
        socket.set("transports", ["websocket"]);
        socket.set("log level", 2);
    });

    setEventHandlers();

    socket = io.connect("http://localhost", {port: 8000, transports: ["websocket"]});

};
Share Improve this question asked Apr 1, 2016 at 23:20 Adil.RAdil.R 1801 gold badge5 silver badges19 bronze badges 1
  • did you called init function in your js file. – Nivesh Commented Apr 2, 2016 at 1:22
Add a ment  | 

1 Answer 1

Reset to default 2

Note: socket = io.connect("http://localhost", {port: 8000, transports: ["websocket"]}); this has to be included in the client side javascript file where you load socket.io.js via script tag.

Change to this:

var util = require("util"),
    io = require("socket.io")({
      transports  : [ 'websocket' ]
    }),
    Player = require("./Player").Player,
    socket,
    players;

function init(){
    players = [];

    socket = io.listen(8000);

    setEventHandlers();

};
init();

Note Below is not supported by socket.io v1.0 you have to install v0.9 if you want to do it that way, use: $ npm install [email protected] -S

socket.configure(function() {
    socket.set("transports", ["websocket"]);
    socket.set("log level", 2);
});

Looging socket.io v1.0 log-level option is removed. Thus for logging one has to start program using debug module.

  1. install debug: npm install debug -S
  2. then run the program: DEBUG=* node entry_file.js
发布评论

评论列表(0)

  1. 暂无评论