javascript - Uncaught TypeError: Object #<Object> has no method 'connect' - Stack Overflow

So I did npm install express and npm install socket.io and I brought the node_modules folders to my ser

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.

Using this as 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);

and using this as client.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ".dtd">
<html xmlns="" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <title>Untitled 4</title>
<script src=".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>

the error I get is Uncaught TypeError: Object #<Object> has no method 'connect'

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.

I also started my server and I got socket.io started so I know that works.

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.

Using this as 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);

and using this as client.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/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>

the error I get is Uncaught TypeError: Object #<Object> has no method 'connect'

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.

I also started my server and I got socket.io started so I know that works.

Share Improve this question asked May 14, 2012 at 19:07 testtest 18.2k67 gold badges173 silver badges245 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 4

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>

in this:

<script src="http://localhost:3000/socket.io/socket.io.js"> </script>

And yes, socket.io is a folder into the root server directory and my server-side socket is listening on port 3000.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745147460a4613710.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信