所以我在Python上使用以下代码创建了一个websocket连接:
channels_dict = {}
channels_dict['Authorization'] = 'true'
for channel in channels: #adds some extra headers with info
channels_dict["Channel" + str(channel)] = '1'
ws = websocket.WebSocketApp("ws://localhost:8888/ws",
on_message = on_message,
on_error = on_error,
on_close = on_close,
header = channels_dict)
我可以轻松添加额外的标头,以便以后在连接时可以在服务器中访问。有没有办法可以在Javascript中做同样的事情?我没有找到有关在websocket创建中设置自定义标头的太多信息。我在JS中有以下代码:
var webSocket;
function openWebsocket() {
webSocket = new WebSocket("ws://localhost:8888/ws")
}
我听说在url中添加查询参数,这是在JavaScript中向Websocket连接添加额外的标头/信息的唯一方法吗?
答案 0 :(得分:1)
尝试一下:
const webSocket = new WebSocket ("ws://localhost:8888/ws" + "?param1=" + param1); // here you can add other params
webSocket.on("open", function open(e) {
console.log("*** websocket opened");
});