douqi2804 2017-12-04 14:38
浏览 1281
已采纳

在本地运行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 15: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")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序