未捕获的TypeError:Object#< Object>没有方法'连接' [英] Uncaught TypeError: Object #<Object> has no method 'connect'
问题描述
所以我做了 npm install express
和 npm install socket.io
我带了 node_modules
我的服务器
文件夹,我使用 WAMP
。
So I did npm install express
and npm install socket.io
and I brought the node_modules
folders to my server
folder where I am using WAMP
.
将此用作 server.js
:
var app = require('express').createServer();
var io = require('socket.io').listen(app);
io.sockets.on('connection', function (socket) {
console.log('Someone connected!');
socket.on('set nickname' , function (nickname) {
socket.nickname = nickname;
console.log(nickname + ' just connected!');
});
});
app.listen(8080);
并将其用作 client.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Untitled 4</title>
<script src="http://cdn.socket.io/stable/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:8080');
socket.on('connect', function () {
console.log('We are connected!');
var name = prompt('What is your name?');
this.emit('set namename', name, function (success) {
console.log('The server got the message!');
if (!success) {
console.log('Nickname is in use!');
}
});
});
</script>
</head>
<body>
</body>
</html>
我得到的错误是未捕获TypeError:对象#<对象>没有方法'connect'
我知道我使用的不同于 socket.io.js
因为我不知道从哪里得到 /socket.io/socket.io.js
。我尝试了 node_modules / socket.io / lib / socket.io.js
这是我发现的另一个我的 node_modules
文件夹,但不是这样。
I know I used a different than socket.io.js
becase I don't know where to get the /socket.io/socket.io.js
. I tried doing node_modules/socket.io/lib/socket.io.js
which is another one I found i my node_modules
folder, but that isn't it.
我还启动了服务器
,我得到了 socket.io已启动
所以我知道这有效。
I also started my server
and I got socket.io started
so I know that works.
推荐答案
你得到的错误是因为socket.io脚本(到index.html)的来源是错误的。所以改变这个:
The error you got is because the source of socket.io script (into index.html) is wrong. So change this:
<script src="http://cdn.socket.io/stable/socket.io.js"></script>
在此:
<script src="http://localhost:3000/socket.io/socket.io.js"> </script>
是的,socket.io是根服务器目录中的文件夹,我的服务器端套接字是在港口3000上听。
And yes, socket.io is a folder into the root server directory and my server-side socket is listening on port 3000.
这篇关于未捕获的TypeError:Object#< Object>没有方法'连接'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!