本地运行 web-Socket 进行调试

2024-02-26

我正在使用 gorilla web socket,我想在本地运行它,我的意思是使用以下 chrome 客户端 或其他推荐的工具...当我进入调试模式时出现错误

I use

"github.com/gorilla/websocket"


var upgrader = websocket.Upgrader{
    ReadBufferSize:  1024,
    WriteBufferSize: 1024,
}

upgrader.CheckOrigin = func(r *http.Request) bool { return true }

c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
    log.Print("upgrade:", err)
    return
}

当我在 Chrome 或 Web 套接字客户端中运行以下网址时,出现错误

websocket:不是 websocket 握手:在“连接”标头中找不到“升级”令牌

localhost:8081/mypath

我想运行它

ws://localhost:8081/mypath

并提供本地模拟的令牌,我该怎么做?

为了检查它,我使用 Chrome 的简单 WebSocket 客户端。任何其他客户都会有帮助

EDIT:

当我在 chrome 控制台中尝试时,出现以下错误:

VM42:164 拒绝连接到“ws://localhost:8081/mypath”,因为它 违反以下内容安全策略指令:“connect-src 'self' uploads.github.com status.github.com Collector.githubapp.com api.github.com www.google-analytics.com github-cloud.s3.amazonaws.com github-生产-存储库-文件-5c1aeb.s3.amazonaws.com github-生产-上传-清单-文件-7fdce7.s3.amazonaws.com github-生产-用户-资产-6210df.s3.amazonaws.com wss://live.github.com


浏览器在获取网页进行显示时不使用 WebSocket 协议。需要编写代码才能从浏览器使用 WebSocket 端点。

大猩猩套装包括examples https://github.com/gorilla/websocket/tree/master/examples展示如何从浏览器连接(chat https://github.com/gorilla/websocket/blob/b89020ee79b89a7f932c5617d218fc06db382f53/examples/chat/home.html#L31-L45 and command https://github.com/gorilla/websocket/blob/b89020ee79b89a7f932c5617d218fc06db382f53/examples/command/home.html#L31-L45例子是很好的起点)。

您可以使用浏览器控制台连接到 WebSocket 端点:

> ws = new WebSocket("ws://localhost:8080/mypath")
> ws.onmessage = function(ev) { console.log(ev.data) }
> ws.send("hello")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

本地运行 web-Socket 进行调试 的相关文章

随机推荐