Node聊天程序实例03:chat.js
作者:vousiu
出处:http://www.cnblogs.com/vousiu
本实例参考自Mike Cantelon等人的《Node.js in Action》一书。
chat.js
这个程序在客户端浏览器运行。
定义一个Chat类,给定一个socket,它可以往这个socket发送事件。
var Chat = function(socket) {
this.socket = socket;
}
指定socket。
Chat.prototype.sendMessage = function(room, text) {
var message = {
room: room,
text: text
};
this.socket.emit('message', message);
};
定义sendMessage成员。
{room: 'roomName', text: 'text'} <---------- 'message' ----------- chat
Chat.prototype.changeRoom = function(room) {
this.socket.emit('join', {
newRoom: room
});
};
定义changeRoom成员。
{newRoom:'roomName'} <------- 'join' -------- chat
Chat.prototype.processCommand = function(command) {
var words = command.split(' ');
var command = words[0].substring(1, words[0].length).toLowerCase();
var message = false;
switch(command) {
case 'join':
words.shift();
var room = words.join(' ');
this.changeRoom(room);
break;
case 'nick':
words.shift();
var name = words.join(' ');
this.socket.emit('nameAttempt', name);
break;
default:
message = 'Unknown command';
break;
}
return message;
};
定义处理命令成员。
若命令为join,则changeRoom;
若命令为nick,则name <------- 'nameAttempt' -------- chat
否则,返回错误信息。
Node聊天程序实例03:chat.js的更多相关文章
- Node聊天程序实例01
作者:vousiu 出处:http://www.cnblogs.com/vousiu 本实例参考自Mike Cantelon等人的<Node.js in Action>一书. 本实例要实现 ...
- Node聊天程序实例06:server.js
作者:vousiu 出处:http://www.cnblogs.com/vousiu 本实例参考自Mike Cantelon等人的<Node.js in Action>一书. server ...
- Node聊天程序实例04:chat_ui.js
作者:vousiu 出处:http://www.cnblogs.com/vousiu 本实例参考自Mike Cantelon等人的<Node.js in Action>一书. 这个程序在客 ...
- Node聊天程序实例02:chat_server.js
作者:vousiu 出处:http://www.cnblogs.com/vousiu 本实例参考自Mike Cantelon等人的<Node.js in Action>一书. chat_s ...
- Node聊天程序实例05:index.html和style.css
作者:vousiu 出处:http://www.cnblogs.com/vousiu 本实例参考自Mike Cantelon等人的<Node.js in Action>一书. index. ...
- 小程序实例:用js方法splict()、indexOf()、push()、replace()等操作数组Array的增删改查
一.增加数组子级 1.Array.push() 定义和用法 向数组的末尾处添加一个或多个子集,并返回新数组的长度 语法 var array=["好","扎在那个" ...
- WinSocket聊天程序实例(多线程)
#pragma comment(lib,"Ws2_32.lib") #include <stdio.h> #include <Winsock2.h> SOC ...
- boost asio异步读写网络聊天程序client 实例具体解释
boost官方文档中聊天程序实例解说 数据包格式chat_message.hpp <pre name="code" class="cpp">< ...
- boost asio异步读写网络聊天程序客户端 实例详解
boost官方文档中聊天程序实例讲解 数据包格式chat_message.hpp <pre name="code" class="cpp">< ...
随机推荐
- 安装eclipse的hadoop开发环境--2
在eclipse上做好一切,在网上很容易搜到 尝试链接hadoop集群失败,尝试单机的操作,发现# ssh localhost失败 解决ssh问题:成功解决 但是eclipse的DFS locatio ...
- vue.js在windows本地下搭建环境和创建项目
Vue.js是一套构建用户界面的渐进式框架.与其他重量级框架不同的是,Vue 采用自底向上增量开发的设计.Vue 的核心库只关注视图层,并且非常容易学习,非常容易与其它库或已有项目整合.另一方面,Vu ...
- Leetcode: Encode String with Shortest Length && G面经
Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...
- LA 3942 - Remember the Word (字典树 + dp)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- C/C++ 结构体 指针 简单输入输出
#include <stdio.h> #include <stdlib.h> struct student{ int num; ]; double dec; }; int ma ...
- 在IE下,如果在readonly的input里面键入backspace键,会触发history.back()
在IE下,如果在readonly的input里面键入backspace键,会触发history.back(), 用以下jQuery代码修正之 $("input[readOnly]" ...
- 使用JFinal的第一个项目出现的问题(The return type is incompatible with JspSourceDependent.getDependants())
四月 08, 2016 4:35:34 下午 org.apache.catalina.core.ApplicationDispatcher invoke严重: Servlet.service() fo ...
- Apache2.4开启GZIP功能
HTTP协议上的GZIP编码是一种用来改进WEB应用程序性能的技术.大流量的WEB站点常常使用GZIP压缩技术来让用户感受更快的速度.这一般是指WWW服务器中安装的一个功能,当有人来访问这个服务器中的 ...
- Bare Medal on BCM2835 and BCM2836
A few days ago, I have tried to write bare medal program but failed. Now I find that the main mistak ...
- linux虚拟机上不了网--桥接方式--问题一直未解决
转载的:可是自己的虚拟机就是上不了网,无线网卡该配的也配了还是不行,如果真有台物理机器就行了,省了好多事:但是模拟性能时肯定不行了:有人知道是什么原因不? 虚拟机网络模式 无论是vmware,virt ...