JSON messaging pattern

blov · · 418 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello people, I am trying to make game server on GO and stuck with messaging. I feel like i am digging in wrong direction... maybe someone can help me? So, i have JS client and GO server. They should communicate through websockets. And here comes the question: Is there any best practices or pattern how to organize this communication? For example i will have 30 different messages with different data. Something like this:</p> <pre><code>{ &#34;command&#34;: &#34;HELLO&#34;, &#34;username&#34;: &#34;superuser&#34;, &#34;token&#34;: &#34;ASdsdasdasd&#34; } </code></pre> <p>And another one:</p> <pre><code>{ &#34;command&#34;: &#34;MOVE&#34;, &#34;x&#34;: 10, &#34;y&#34;: 10 } </code></pre> <p>On backend i have <em>websocket.Conn</em> connection and i am reading from it like <em>websocket.json.Receive(ws, &amp;msg)</em>. But i can`t figure out how can i read completely different types of messages.</p> <hr/>**评论:**<br/><br/>icholy: <pre><p>I&#39;d do something like this: <a href="https://play.golang.org/p/E8hEK8YoPT" rel="nofollow">https://play.golang.org/p/E8hEK8YoPT</a></p></pre>nsd433: <pre><p>The two suggestions above are good. If you can&#39;t change the JSON, there&#39;s one more way: unpack twice. The first time you unpack into a struct type that just has one field, Command, and then switch on that and unpack again.</p> <p>This works because the json.Unmarshal ignores fields which aren&#39;t present in both the JSON and the target type.</p></pre>materialdesigner: <pre><p>I&#39;m glad someone else says this!</p> <p>One of the most <em>powerful</em> aspects of the way golang does json deserialization is in how the same json string can be parsed into however many data types you need. Some people make monolithic parse types. No need! Use the pieces you need!</p></pre>rogeralsing: <pre><p>Use an envelope type that knows about all your messages. e.g. it can have a field for each known message type.</p> <p>{ &#34;hello&#34; : {....} &#34;move&#34; : {....} }</p> <p>This is how protobuf does it when you use the &#34;oneof&#34; directive. you would then have to check for nil on each of those fields.</p> <p>Maybe there is something more fancy for Go JSON, but thats the proto way at least.</p></pre>icholy: <pre><p>That&#39;s actually pretty clever ...</p></pre>tmornini: <pre><p>That is clever indeed!</p> <p>Thanks!</p> <p><a href="/u/ChangeTip" rel="nofollow">/u/ChangeTip</a> $1</p></pre>changetip: <pre><p><em>rogeralsing</em> received a tip for 2,201 bits ($1.00).</p> <h2></h2> <p><a href="https://www.reddit.com/r/changetip/wiki/tipping-on-reddit" rel="nofollow"><sup><sup>what</sup></sup> <sup><sup>is</sup></sup> <sup><sup>ChangeTip?</sup></sup></a></p></pre>tv64738: <pre><p>I wrote an article about that: <a href="http://eagain.net/articles/go-dynamic-json/" rel="nofollow">http://eagain.net/articles/go-dynamic-json/</a></p> <p>Also likely useful: <a href="https://whispering-gophers.appspot.com/" rel="nofollow">https://whispering-gophers.appspot.com/</a></p></pre>

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

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