My First Project - The WebSocket Protocol - Would really appreciate some feedback

xuanbao · · 486 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi there gophers :)</p> <p>I&#39;ve uploaded my first Go project on Github and I would really like some feedback. I&#39;m still fairly new to the language but I&#39;m already in love with it!!</p> <p>Github: <a href="https://github.com/tabone/websocket" rel="nofollow">https://github.com/tabone/websocket</a></p> <p>Thanks in advance!! taboneio</p> <hr/>**评论:**<br/><br/>kardianos: <pre><p>I&#39;m glad you like go!</p> <p>Have you seen godoc.org? <a href="https://godoc.org/?q=websocket" rel="nofollow">https://godoc.org/?q=websocket</a></p> <p>The two most used I think are:</p> <ul> <li><a href="https://godoc.org/github.com/gorilla/websocket" rel="nofollow">https://godoc.org/github.com/gorilla/websocket</a></li> <li><a href="https://godoc.org/golang.org/x/net/websocket" rel="nofollow">https://godoc.org/golang.org/x/net/websocket</a></li> </ul> <p>Just a skim, but I would prefer &#34;//&#34; to &#34;/* */&#34; for struct field docs. Also &#34;func (s *Socket) Write(o int, p []byte) error&#34; prevents using something like io.Copy on it. Maybe set that elsewhere?</p> <p>You might also want to type your constants. &#34;type Op int&#34; &#34;const OpText Op = 1&#34;</p></pre>taboneIO: <pre><p>Hi kardianos,</p> <p>Thanks for taking the time to give me some tips!!</p> <p>To tell you the truth, initially I used /**/ but for some reason &#39;goreportcard&#39; was having some problems parsing the docs.</p> <p>Regarding the Write() method, I would like to make it implement the io.Writer interface however since a frame can take different forms (ex. Text, Binary, Ping, etc...), the opcode its kind of required. Maybe I should have picked a better method name.</p> <p>Regarding your third tip (Type Constants) is there a good reason to do it? (sorry for asking :s I don&#39;t want to sound rude). By using built in types makes the process of sending a message easier and shorter.</p> <p>Thanks for your time!</p></pre>kardianos: <pre><p>Write: set it on your stream (set mode), then remove from write.</p> <p>Constants: programmer documentation, prevent from making silly mistakes.</p></pre>synalx: <pre><p>So, for the <code>Write()</code> method, I agree that a better method name would be ideal. Maybe <code>WriteMessage()</code>? It&#39;s definitely strange to see a method with that name that doesn&#39;t satisfy the <code>Writer</code> interface.</p> <p>Here&#39;s one idea on how to do this:</p> <pre><code>type SocketBinaryWriter struct { socket *Socket } func (sbw SocketBinaryWriter) Write(payload []byte) (n int, err error { return sbw.socket.WriteMessage(OpcodeBinary, payload) } func (s *Socket) BinaryWriter() SocketBinaryWriter { return SocketBinaryWriter{s} } </code></pre> <p>This way you can get an <code>io.Writer</code> for writing binary data to the <code>Socket</code>, but also expose the lower level interface for other opcode types (which could have their own wrappers where appropriate).</p> <p>Typed constants: it&#39;s very important that you do this. Seeing (o int) I have no idea what is valid to pass, but seeing (o Opcode) I know instantly to look for Opcode* constants of the right type.</p></pre>mwholt: <pre><p>Props for implementing a protocol. That&#39;s great practice and a super important skill to have!</p></pre>

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

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