How to send messages to client in real-time?

xuanbao · · 536 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>My team and I are considering using Go for our new project in school. We want to create an Android application, which would be informed by the server about new events. How would we achieve this? I already read a bit about Google Cloud Messaging, but we would like to avoid that and would prefer to an XMPP library or just plain sockets. </p> <p>Are plain sockets from golang&#39;s net package a good idea? And how could we keep track of all the connections and send messages to the right people? Almost all of us are completely new to Go and haven&#39;t done much socket programming, so we don&#39;t really know where to begin. If someone could point us in the right direction, that would be great.</p> <hr/>**评论:**<br/><br/>Injunire: <pre><p>You could use a server that supports AMQP for message passing.</p> <p>I&#39;ve used RabbitMQ for messaging between servers and applications and it works very well. They have a library built for Go and some great tutorials on their website on how to create messages and queues.</p></pre>xiaojiemi: <pre><p>How about grpc( use for connect the client and the server), take a look at grpc.io, You may still need some message queue system. I don&#39;t know if it is fit, just provide information for you to check out</p></pre>v36372: <pre><p>afaik, the standard socket library is fine. I&#39;m using it for my own project. Speaking of socket, you would have websocket and tcp socket. At first, I started with tcp, and it worked great. However, my front end (AngularJS) cant have a tcp setup up and running so I switched to websocket. It&#39;s working fine now. Be my guest to checkout my repo for reference. Take a look at server.go. Sorry for messy code, I havent review it yet. <a href="https://github.com/v36372/thesis" rel="nofollow">https://github.com/v36372/thesis</a></p></pre>mko31: <pre><p>Is WebSocket really an option for Android? I think TCP sockets seem like the way to go.</p></pre>v36372: <pre><p>Feel free to go for your thought. I just want to remind you about how to manage clients and send the right message to the right ones, as mentioned in your question. You should take a look at some design pattern for handling connections, as in my case, I used a manager struct and take advantage of the golang concurrency feature.</p></pre>v36372: <pre><p>if you want to check out the tcp method, you can look them up at this commit of mine <a href="https://goo.gl/poI9ov" rel="nofollow">https://goo.gl/poI9ov</a>. Or you can just google it, there s alot of materials out there.</p></pre>mko31: <pre><p>I looked at your commit, which was very helpful, and I think I almost understand everything that&#39;s happening in your code right now. The only thing I do not understand is the float64 variable called out and the the mutex in Manager. What are they used for? Could you further elaborate on that?</p></pre>nerdwaller: <pre><p>Yes, there are several implementations, with varying limitations. Iirc last one I used was <a href="http://socket.io/blog/native-socket-io-and-android/" rel="nofollow">Socket.io</a></p></pre>

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

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