douqi2804 2017-12-04 06:38
浏览 1286
已采纳

在本地运行Web-Socket进行调试

I’m using gorilla web socket and I want to run it locally , I mean with the following chrome client or other recommended tool …when I run into debug mode I got error

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
}

When I run the following url in chrome or web socket client I got error

websocket: not a websocket handshake: 'upgrade' token not found in 'Connection' header

localhost:8081/mypath

and I want to run it

ws://localhost:8081/mypath

and provide token for local simulation, how I can do it ?

To check it I use Simple WebSocket Client of chrome. any other client will be helpful

EDIT:

when I try it in the chrome console I got the following error:

VM42:164 Refused to connect to 'ws://localhost:8081/mypath' because it violates the following Content Security Policy directive: "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-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com wss://live.github.com

展开全部

  • 写回答

1条回答 默认 最新

  • duanjiao3686 2017-12-04 07:12
    关注

    Browsers do not use the WebSocket protocol when fetching a web page for display. Code is required to use a WebSocket endpoint from a browser.

    The Gorilla package includes examples showing how to connect from a browser (the chat and command examples are good places to start).

    You can can connect to a WebSocket endpoint using the browser console:

    > ws = new WebSocket("ws://localhost:8080/mypath")
    > ws.onmessage = function(ev) { console.log(ev.data) }
    > ws.send("hello")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部