问题描述
我正在关注使用节点js和socket.io进行的简单多人游戏的教程( http://rawkes.com/articles/creating-a-real-time-multiplayer-game-with-websockets-and-node.html ) .我在尝试连接到套接字服务器时遇到问题,出现错误提示TypeError:io.connect不是函数PS:我是Node js和socket.io的初学者,所以请帮帮我.
I am following a tutorial for a simple multiplayer game using node js and socket.io (http://rawkes.com/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 sayingTypeError: io.connect is not a functionPS : 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"]});
};
推荐答案
注意: socket = io.connect("http://localhost", {port: 8000, transports: ["websocket"]});
这必须包含在通过脚本标记加载socket.io.js
的客户端javascript文件中.
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.
更改为此:
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();
注意 socket.io v1.0不支持以下版本,如果要这样做,则必须安装v0.9,请使用:$ npm install [email protected] -S
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);
});
懒惰 socket.io v1.0 log-level
选项已删除.因此,要记录日志,必须使用调试模块启动程序.
Looging socket.io v1.0 log-level
option is removed. Thus for logging one has to start program using debug module.
- 安装调试:
npm install debug -S
- 然后运行程序:
DEBUG=* node entry_file.js
- install debug:
npm install debug -S
- then run the program:
DEBUG=* node entry_file.js
这篇关于TypeError:io.connect不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!