<p>I've ran into some strange websockets behaviour today and seek for some help from comunity to understand what can be the cause.</p>
<p>I've been trying to implement some simple webservice using websockets (from gorillas toolkit). Everything worked fine at home (I'm using macOS there), but when I pulled the latest version from git at the office (we have Windows here) - I was surprised to see that the very same code gives different result. After some time connection timeouts, pushing all messages to the client, and closes.</p>
<p>After some meddling with my code I couldn't figure out what was wrong and decided to test gorilla's chat example (<a href="https://github.com/gorilla/websocket/tree/master/examples/chat" rel="nofollow">https://github.com/gorilla/websocket/tree/master/examples/chat</a>)</p>
<p>Same behavior. You send some messages to chat, no reaction, 60 seconds after - you finally see:</p>
<pre><code>test1
test2
test3
Connection closed.
</code></pre>
<p>Any ideas what can cause this? </p>
<p>My repo (just in case): <a href="https://github.com/konart/gokoi/tree/websockets" rel="nofollow">https://github.com/konart/gokoi/tree/websockets</a></p>
<hr/>**评论:**<br/><br/>konart: <pre><p>Okay I figured it out. All ports except for 15000 are closed. </p>
<p>Mark as SOLVED. :)</p></pre>SilentWeaponQuietWar: <pre><p>the <strong>ListenAndServe</strong> method doesn't let you explicitly set timeouts and other config settings. For that very reason, it's usually suggested to use <strong>http.Server</strong> instead</p>
<p><a href="https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/" rel="nofollow">https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/</a></p></pre>konart: <pre><p>I don't think this is the case though. First of all timeout happens after 60 seconds, so I believe this is the "// Time allowed to read the next pong message from the peer." or a "pongWait" variable that is used for setting ReadDeadline and PongHandler.</p>
<p>Anyway - I've tried doing it this way before (just in case):</p>
<pre><code>srv := &http.Server{
Handler: r,
Addr: "127.0.0.1:8000",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(srv.ListenAndServe())
</code></pre>
<p>Same outcome.</p></pre>daveddev: <pre><p>If you are using a reverse proxy to pass off processing, you may be running into a timeout defined as a default within that system. This came up as a problem for me recently (nginx - proxy_read_timeout). I hope this helps.</p></pre>konart: <pre><p>Thanks, I'm gonna keep it in mind, but for now - I figured it out. Blocked ports were the problem</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传