socket伺服器 前後端連線

2022-07-25 01:51:09 字數 769 閱讀 2259

客戶端

html結構

"

text

">

"sendserver()

">send

js**

const ws = new websocket('

ws://localhost:8080/

')//

引數是要連線的伺服器位址

//已經連線

ws.onopen = () =>

//前端接受伺服器訊息

ws.onmessage = (msg) =>

//錯誤的時候觸發

ws.onerror = (err) =>

//伺服器斷開連線的時候觸發

ws.onclose = () =>

//前端向後端發訊息

function sendserver()

後端伺服器

const websocket = require('ws'

)const ws = new websocket.server(,()=>)

//開啟乙個socket 伺服器

ws.on('

connection

', (client) =>)

'close', () => )

})//

function broadcast(client, msg) //}

注釋**為根據需要而用

socket伺服器簡易連線

1.socket 建立socket偽檔案生成檔案描述符fd 2.bind 給fd繫結埠和ip 3.listen 給當前socket設定最大客戶端鏈結 4.accept 阻塞等待客戶端連線 生成客戶端的fd,addr結構體 客戶端埠,ip等內容 5.read 讀資料 6.write 寫資料 7.clo...

後端 伺服器

開啟檔案nginx.conf檔案 做出以下修改 server 404頁面配置,頁面同樣在html資料夾中 error page 404 404.html location 404.html 其他錯誤碼頁面配置 error page 500 502 503 504 50x.html location ...

後端 伺服器

開啟檔案nginx.conf檔案 做出以下修改 server 404頁面配置,頁面同樣在html資料夾中 error page 404 404.html location 404.html 其他錯誤碼頁面配置 error page 500 502 503 504 50x.html location ...