Simple Chat Server in Go

agolangf · · 1488 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I created a very simple chat server that uses websockets, and clients can simply connect using a browser: <a href="https://github.com/indy-golang/simple-chat">github/indy-golang/simple-chat</a></p> <p>I made this for a presentation for Indy (Indianapolis) Golang meetup. If you live in the Indianapolis area you are welcome to come! <a href="http://www.meetup.com/Indy-Golang/events/221026891/">Indy Golang April 7 meeting</a></p> <hr/>**评论:**<br/><br/>drwiggly: <pre><p>Nice, fyi i don&#39;t think the join and leave are safe. You&#39;re accessing the map directly should probably have a RWMutex for add remove (write), and use read when doing the broadcast.</p></pre>elcct: <pre><p>Or channels like in my chat application:</p> <p><a href="https://github.com/elcct/taillachat/blob/master/controllers/chat.go" rel="nofollow">https://github.com/elcct/taillachat/blob/master/controllers/chat.go</a></p></pre>fprotthetarball: <pre><p>This is interesting, but it seems like you&#39;re introducing a global lock on all actions by way of the channel. Locking on just the map access should be more performant.</p></pre>koffiezet: <pre><p>Channels will probably be slower...</p></pre>lacikawiz: <pre><p>Thank you for the tips! I didn&#39;t think of a race condition there.</p></pre>ricoxg: <pre><p>That&#39;s a fantastic program for teaching a slew of Golang concepts, and wish I&#39;d seen it back when I was first learning. I&#39;ll definitely be sharing it out with a few fellow neophytes for reference.</p> <p>Thanks for sharing!</p></pre>UserNumber42: <pre><p>I have a question. How would you go about securing something like this? If you&#39;re using js for the websocket, doesn&#39;t that mean every bit of information you send to the client is viewable by someone with even the most basic of knowledge? If you tried to implement something using tokens or salts all that information is accessible by the client.</p></pre>gogolang: <pre><p>Looks interesting! To make it more &#34;Go&#34; like, you should use a fully qualified path to the websocket package.</p> <p>I wasn&#39;t able to run go get github.com/indy-golang/simple-chat</p> <p>local import &#34;./websocket&#34; in non-local package</p></pre>lacikawiz: <pre><p>I&#39;m fully aware that it&#39;s not a good practice but I wanted to make it dependency free. </p> <p>To be honest, I do not like the way <code>go get</code> works, and I&#39;m eagerly looking forward to the solution to the vendoring debate.</p></pre>avrtno: <pre><p>There is no debate that absolute paths should be used when vendoring. Change the import to &#34;github.com/indy-golang/simple-chat/websocket&#34;. </p></pre>robvdl: <pre><p>I am just wondering, why on earth would you write a websocket chat server from scratch, instead of just using gorilla websockets?</p></pre>om0tho: <pre><p>Because you can&#39;t do this.</p> <pre><code>import &#34;github.com/gorilla/websocket&#34; func main() { websocket.ChatServer() } </code></pre> <p>Gorilla websockets just buys you the websocket connection, not the whole chat server.</p></pre>robvdl: <pre><p>Sure, but I guess my question is, why re-invent the wheel and do the low-level websocket code again, when you could just build the chat server on top of existing gorilla websockets code. I guess I didn&#39;t quite word my question that well.</p></pre>Bromlife: <pre><p>Really cool. When I first started looking into coding in Go this was the exact kind of thing I looked for to learn goroutines, but I couldn&#39;t find anything that helped. This would have been perfect. Chatservers make sense for learning concurrency. </p></pre>mgutz: <pre><p>Here&#39;s a good article on the subject that explains the basics </p> <p><a href="http://gary.burd.info/go-websocket-chat">http://gary.burd.info/go-websocket-chat</a> </p></pre>lacikawiz: <pre><p>Thanks! </p> <p>I hope it will be useful for other future Gophers :-)</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1488 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传