静态中英文网站怎么做,兼职网站编辑,申请注册网站域名.商城,字画网站建设Node中的net模块提供的前端通信 H5提供的 webSocket 【 常用于 移动端 】pc端低版本浏览器使用 socket.io 通信- 服务端#xff1a; 总的服务器 举例#xff1a; 腾讯的qq服务器 1个整体- 客户端#xff1a; 单个用户使用应用 举例#xff1a; 每个人的qq 多个client.js 客… Node中的net模块提供的前端通信 H5提供的 webSocket 【 常用于 移动端 】 pc端低版本浏览器使用 socket.io 通信 - 服务端 总的服务器 举例 腾讯的qq服务器 1个整体 - 客户端 单个用户使用应用 举例 每个人的qq 多个 client.js 客户端 业务客户端现在要在终端输入内容然后回车发送内容给服务器 解决 Node中提供了一个叫做 readline 的 模块用于读取命令行内容 【 单行读取 】 const net require( net ) const socket net.Socket() const host localhostconst port 5000 const readline require( readline) socket.connect( port,host, () { socket.write( hello 我上线了 ) })const rl readline.createInterface({ input: process.stdin, output: process.stdout});socket.on(data, msg { console.log( msg.toString() ) say()})socket.on( error, ( error ) { console.log( error is : error )})socket.on( close, () { console.log( 客户端下线了 )})function say () { rl.question(请输入, ( answer ) { if( answer bye ){ socket.destroy() rl.end() }else{ socket.write( answer ) } });} //服务端server.js const net require( net ) const server net.createServer()const host localhost const port 5000 let count 0 const clients {} server.on(connection, ( client ) { client.name count clients[ client.name ] client client.on( data, msg { console.log( 客户端${ client.name }说${ msg.toString() } ) boardCaster( client,msg ) }) client.on( error, error { console.log( error is: error ) }) client.on( close, () { delete clients[ client.name ] console.log( 客户端${ client.name }下线了 ) })})function boardCaster ( client,msg ) { for( var key in clients ){ // clients[ key ].write( 谁说了什么 ) clients[ key ].write( 客户端${ client.name }说${ msg.toString() } ) }}转载于:https://www.cnblogs.com/yangjin0514/p/11205979.html