聊天室三種寫法

2021-09-25 09:25:19 字數 2168 閱讀 4194

const net = require( 'net' ) //引入net模組

const socket = net.socket() //建立客戶端

const host = 'localhost'

const port = 5000

const readline = require( 'readline') // 讀取命令列

// 客戶端連線伺服器

socket.connect( port,host, () => )

const rl = readline.createinte***ce();

socket.on('data', msg => )

socket.on( 'error', ( error ) => )

socket.on( 'close', () => )

function say () else

});}

server

const net = require( 'net' ) //引入net模組

const server = net.createserver() // 建立伺服器

const host = 'localhost' // 建立 網域名稱

const port = 5000 // 建立埠

let count = 0

const clients = {}

server.on('connection', ( client ) => 說:$` )

boardcaster( client,msg )

})client.on( 'error', error => )

client.on( 'close', () => 下線了` )

})})function boardcaster ( client,msg ) 說:$` ) // 寫資訊在服務端

}}// 監聽伺服器

提交js

socket.io.js

只適用於老的瀏覽器,現在已經不推薦使用了

伺服器

const websocket = require( 'ws' ) // 引入ws模組

// 建立伺服器

// const ws = new websocket( options ) options就是乙個物件

const ws = new websocket.server()

let count = 0

const clients = {}

ws.on('connection', client => 說:$` )

boardcaster( client,msg )

})client.on('close', () => 下線了` )

})})function boardcaster ( client,msg ) 說: $` )

}}靜態伺服器

const express = require( 'express' )

const path = require( 'path' )

// 理解: 為了呼叫中介軟體( 函式 )

const port = 8000

const host = '10.31.158.25'

// console.log( __dirname ) // 當前檔案所在的磁碟路徑

console.log( `伺服器執行在: http://$:$` )

})連線通訊伺服器 js

const url = 'ws:'

const ws = new websocket( url )

ws.onopen = () =>

ws.onmessage = msg =>

http部分

var submit = document.queryselector('#submit')

var msg = document.queryselector('#msg')

// submit.onclick = function ()

document.onkeyup = function ( e )

}

mysql 聊天室 聊天室php mysql 六

聊天室php mysql 六 相應的 資料庫 phpmyadmin mysql dump 主機 localhost 3306 資料庫 study28 資料表的結構 chat user create table chat user userid varchar 20 not null,passwd v...

聊天室程式

伺服器 include include include include include include include include define servport 8081 伺服器端口號 define bufsize 200 最大傳輸量 int main int args,char argv s...

簡單聊天室

include include include include include include include include include include pthread t thread 2 void send msg void ip msg if connect sockfd,struct ...