前端通信
Node中的net模塊提供的前端通信 H5提供的 webSocket 【 常用于 移動端 】 pc端低版本瀏覽器使用 socket.io 通信 - 服務端: 總的服務器 舉例: 騰訊的qq服務器 1個整體 - 客戶端: 單個用戶使用應用 舉例: 每個人的qq 多個 client.js 客戶端 業務:客戶端現在要在終端輸入內容,然后回車發送內容給服務器 解決: Node中提供了一個叫做? readline 的 模塊用于讀取命令行內容 【 單行讀取 】 const net = require( 'net' )
const socket = net.Socket()
const host = 'localhost'
const 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() }` )
? }
}
const socket = net.Socket()
const host = 'localhost'
const 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
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
- 上一篇: 人民邮电出版社图灵公司的两本SQL Se
- 下一篇: 在网页中的flash游戏,按方向键浏览器