【发布时间】:2012-05-22 07:19:03
【问题描述】:
所以我做了npm install express 和npm install socket.io 并将node_modules 文件夹带到我正在使用WAMP 的server 文件夹中。
将其用作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>
我得到的错误是Uncaught TypeError: Object #<Object> has no method 'connect'
我知道我使用的不是socket.io.js,因为我不知道从哪里获得/socket.io/socket.io.js。我尝试做node_modules/socket.io/lib/socket.io.js,这是我在node_modules 文件夹中找到的另一个,但不是这样。
我也开始了我的server,我得到了socket.io started,所以我知道这是可行的。
【问题讨论】:
标签: javascript node.js socket.io