亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從 websocket 連接讀取標頭

如何從 websocket 連接讀取標頭

一只斗牛犬 2021-12-12 11:03:02
我嘗試發送有關 webSocket 標頭的信息,并在連接時在服務器上讀取它。像:客戶端代碼很簡單:ws = await WebSocket.connect('ws://localhost.com:36485', headers: {        'codeName': 'Something',      },);服務器代碼:var WebSocketServer = require('ws').Server  , wss = new WebSocketServer({ port: 36485 });wss.on('connection', function connection(ws) {  console.log(ws.upgradeReq.headers);  ws.on('message', function incoming(message) {    console.log('received: %s', message);  });});我的例外是:類型錯誤:無法讀取未定義的屬性“標題”
查看完整描述

2 回答

?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

如果您在 NPM 上使用這個ws模塊,那么您可以訪問這樣的標頭(直接取自文檔):


const WebSocket = require('ws');

const wss = new WebSocket.Server({ port: 8080 });


wss.on('connection', function(ws, req) {

  console.log(req.headers);

});


查看完整回答
反對 回復 2021-12-12
?
喵喔喔

TA貢獻1735條經驗 獲得超5個贊

由于某種原因它不能與“ws”一起工作,但它與“webSoket”一起工作得很好。


var webSocketServer = require('websocket').server;

var http = require('http');

wsServer.on('request', function(request){

  console.log(request.httpRequest.headers['codename']);

}


查看完整回答
反對 回復 2021-12-12
  • 2 回答
  • 0 關注
  • 247 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號