【发布时间】:2012-09-10 01:34:59
【问题描述】:
我正在做一些需要聊天应用程序的项目。我决定在这里测试一些 node.js/websocket 版本:http://martinsikora.com/nodejs-and-websocket-simple-chat-tutorial
一切都很完美,但正如他在教程末尾提到的那样:
与 Apache 不同,Node.js 不为每个连接使用进程。
表示7个用户登录后,每一个硬编码颜色都会被使用,然后用户名样式使用白色。
// Array with some colors
var colors = [ 'red', 'green', 'blue', 'magenta', 'purple', 'plum', 'orange' ];
// ... in random order
colors.sort(function(a,b) { return Math.random() > 0.5; } );
userName = htmlEntities(message.utf8Data);
// get random color and send it back to the user
userColor = colors.shift();
connection.sendUTF(JSON.stringify({ type:'color', data: userColor }));
console.log((new Date()) + ' User is known as: ' + userName
+ ' with ' + userColor + ' color.');
是否有可能允许两个用户使用相同的颜色? 谢谢
【问题讨论】:
-
如果我使用 .indexOf() 而不是 .shift() 方法会发生什么?
标签: arrays node.js websocket chat process