nodejs学习资源
来源:互联网 发布:网络电视收看电视频道 编辑:程序博客网 时间:2024/04/30 17:53
http://blog.csdn.net/mingcai_xiong/article/details/52669074
nodejs——网络编程模块
http://blog.csdn.net/mingcai_xiong/article/details/52669074 //翻译API
https://nodejs.org/api/net.html#net_socket_destroy_exception //英文API
https://www.oschina.net/code/snippet_113421_36184
nodejs简单的telnet聊天室
var
tcp = require(
'net'
);
var
Server = tcp.createServer();
var
pwd=[];
// keep tracks of connected clients
var
ip=[];
// keeps tracks of connected clients IP address
var
Cpwd =
"1"
;
// password that you require to login
Server.on(
'connection'
,
function
(conn){
conn.setEncoding(
'utf-8'
);
conn.write(
"Password:"
);
// ask user for password on their terminal
console.log(
"["
+ conn.remoteAddress +
"] has joined the chat"
);
conn.on(
'data'
,
function
(data){
if
(pwd.indexOf(conn)>-1){
//check if it is an old client or a new client
console.log(
"["
+ conn.remoteAddress +
"]:"
+ data);
if
(pwd.length > 0){
// check if atleast one client is connected
sendMessage(conn , data);
// broadcast message to all client connected
}
}
else
{
//if it is a new client then server should first check for password
data= data.toString(
'utf-8'
).trim();
var
message =
" has joined the chat"
;
if
(Cpwd == data){
// if it is a new client than check for password
pwd.push(conn);
ip.push(conn.remoteAddress);
sendMessage(conn , message);
}
else
{conn.write(
"Password rejected:"
+ data);conn.end();}
// disconnect client
}
});
conn.on(
'end'
,
function
() {
// remove the client from reference array
var
i , client;
for
(i
in
pwd){
client = pwd[i];
if
(!client.writable){
pwd.splice(i,1);
console.log(ip[i]+
" has left the chat"
);
ip.splice(i,1);
}
}
});
});
function
sendMessage(conn , message){
//function to send message to all connected client
var
i , client;
for
(i
in
pwd){
client = pwd[i];
if
(client.writable){
if
(conn === client){
client.write(
"[me]:"
+ message);
}
else
client.write(
"["
+ conn.remoteAddress +
"]:"
+ message);
}
else
{
pwd.splice(i , 1);
console.log(ip[i]+
" has left the chat"
);
ip.splice(i,1);
}
}
}
Server.listen(8000);
0 0
- nodejs学习资源
- NodeJS学习资源
- 【NodeJS 学习笔记02】入门资源很重要
- nodejs资源
- nodejs资源
- nodejs学习
- NodeJS学习
- 学习nodejs
- nodeJs学习
- NodeJs学习
- nodejs 学习
- nodejs学习
- NodeJS 学习
- nodejs 学习
- nodejs学习
- nodeJs学习
- 学习nodejs
- NodeJS学习
- wps插入I,II,III类的页码
- linux下部署web项目
- 06.探讨JS合并两个数组的方法
- php监控网站访问状态
- 【JSOI2008】最大数 线段树
- nodejs学习资源
- redis 常用命令总结
- linux下画图:modify matplotlib so that we can draw picture under linux
- 欢迎使用CSDN-markdown编辑器
- 洛谷 P3672 小清新签到题(木治啊!!)
- Mysql垂直分区和水平分区
- 操作系统作业03
- Unity3d知识点
- SVN分支/合并原理及最佳实践1