我是socket.io的新手,我正在尝试从一本书Socket.IO Real-Time Web Application Development
中学习它。
有一个使用Node.js和express的聊天应用程序的简单代码。对于服务器端,有sockets.js
代码:
var io = require('socket.io');
exports.initialize = function(server) {
io = io.listen(server);
io.sockets.on("connection", function(socket) {
socket.on('message', function(message) {
message = JSON.parse(message);
if(message.type == "userMessage") {
socket.get('nickname', function(err, nickname) {
message.username = nickname;
socket.broadcast.send(JSON.stringify(message));
message.type = "myMessage";
socket.send(JSON.stringify(message));
});
}
});
socket.on("set_name", function(data) {
socket.set('nickname', data.name, function() {
socket.emit('name_set', data);
socket.send(JSON.stringify(
{type: 'serverMessage',
message: 'Welcome to the chat-room'}
));
});
});
});
}
对于客户端,有chat.js
代码:
var socket = io.connect('/');
socket.on('name_set', function(data) {
$('#nameform').hide();
$('#messages').append('<div class="systemMessage">' + 'Hello ' + data.name + '</div>');
$('#send').click(function() {
var data = {
message: $('#message').val(),
type: 'userMessage'
};
socket.send(JSON.stringify(data));
$('#message').val('');
});
socket.on('message', function(data) {
data = JSON.parse(data);
if(data.username) {
$('#messages').append('<div class="' + data.type + '"><span class="name"'
+ data.username + ":</span> " + data.message + '</div>');
}
else {
$('#messages').append('<div class="' + data.type + '">' + data.message + '</div>');
}
});
});
$(function() {
$('#setname').click(function() {
socket.emit("set_name", {name: $('#nickname').val()});
});
});
这是index.jade
文件:
extends layout
block content
section#chatroom
div#messages
input#message(type='text', placeholder='Enter your message here')
input#send(type='button', value='Send')
section#nameform.modal
div.backdrop
div.popup
div.pophead Please enter a nickname
div.popbody
input#nickname(type='text')
input#setname(type='button', value='Set Name')
当我点击Set Name
按钮时,抛出的错误是:
TypeError: undefined is not a function
at Socket.<anonymous> (/path/to/my/application/routes/sockets.js:20:11)
at Socket.emit (events.js:107:17)
at Socket.onevent (/path/to/my/application/node_modules/socket.io/lib/socket.js:335:8)
at Socket.onpacket (/path/to/my/application/node_modules/socket.io/lib/socket.js:295:12)
at Client.ondecoded (/path/to/my/application/node_modules/socket.io/lib/client.js:193:14)
at Decoder.Emitter.emit (/path/to/my/application/node_modules/socket.io/node_modules/socket.io-parser/node_modules/component-emitter/index.js:134:20)
at Decoder.add (/path/to/my/application/node_modules/socket.io/node_modules/socket.io-parser/index.js:247:12)
at Client.ondata (/path/to/my/application/node_modules/socket.io/lib/client.js:175:18)
at Socket.emit (events.js:107:17)
at Socket.onPacket (/path/to/my/application/node_modules/socket.io/node_modules/engine.io/lib/socket.js:101:14)
该行显示错误
socket.set('nickname', data.name, function() {...});
in sockets.js。
我在本书中使用的唯一不同的是socket.io的版本。本书使用版本0.9.10,而我使用版本1.4.4。这对这个简单的代码有什么不同吗?另外我发现socket.set()已被弃用,但它具有向后兼容性,所以这应该可行。