简体   繁体   English

Websocket聊天实现

[英]Websocket chat implementation

Most of the demos of Websockets I see are of a chatroom application. 我看到的大多数Websockets演示都是一个聊天室应用程序。 I was wondering if it was possible to create more of an Instant Message implementation. 我想知道是否可以创建更多即时消息实现。 The difference being that, in a chatroom application, numerous users connect and share messages with everyone, where, in an instant message application, users can connect to other users they choose. 区别在于,在聊天室应用程序中,许多用户可以与每个人连接并共享消息,而在即时消息应用程序中,用户可以连接到他们选择的其他用户。

I would like it to work without any plug-ins. 我希望它能在没有任何插件的情况下工作。 I'm using JavaScript for the client side and Java EE for the server side. 我在客户端使用JavaScript ,在服务器端使用Java EE I looked into being able to change the endpoint URL but it seems that it has to be known at compile time. 我研究了能够更改终结点URL但似乎必须在编译时就知道它。 I also looked into using the Session object in the onMessage method but how would I know the session object of the user I need to send the message to? 我也研究了在onMessage方法中使用Session对象,但是我如何知道需要向其发送消息的用户的session对象呢? And I feel that would be a slow method to cycle through all session objects looking for the correct one. 我觉得这将是一种缓慢的方法来循环遍历所有会话对象以寻找正确的对象。 So, how could I create an Instant Message like application using WebSockets ? 因此,如何使用WebSockets创建类似应用程序的即时消息?

Websockets are for communication between server and client using the HTTP protocol. Websocket用于使用HTTP协议在服务器和客户端之间进行通信。

They are a particularly fit solution for any cross-language/cross-platform real time streaming/message passing related tasks, because the client(s)/consumers get notified instantly when any new data arrives, without the need of polling**. 它们是任何跨语言/跨平台实时流/消息传递相关任务的特别合适的解决方案,因为当任何新数据到达时,客户端/消费者会立即收到通知,而无需轮询**。

The browser implements the client part of the specification. 浏览器实现规范的客户端部分。

Most server-side languages have libraries implementing the server part. 大多数服务器端语言都有实现服务器部分的库。

If you want basic instant messaging it's as easy as regular chat: you just have to change the list of recipients from everybody in the chat room to the particular client(s) on that conversation. 如果您想要基本的即时消息传递,则与常规聊天一样简单:您只需要将收件人列表从聊天室中的每个人更改为该对话中的特定客户即可。

If you want to build a production grade instant messenger app, you should be aware you don't have to reinvent the wheel: You can built your messenger app on top of any XMPP library or code your own implementation of the protocol. 如果要构建生产级的即时通讯应用程序,则应注意不必重新发明轮子:您可以在任何XMPP库的顶部构建您的Messenger应用程序,或编写自己的协议实现。 Either way it takes away a good part of the design burden, letting you focus on the GUI or whatever extensions you consider appropriate. 无论哪种方式,它都能减轻设计负担的大部分,让您专注于GUI或您认为合适的任何扩展。

If you are interested on the latter. 如果您对后者感兴趣。 check out atmosphere (specially, their plugins and extensions ) and this article (it's a little bit old, but it's good introductory stuff): 查看气氛 (特别是他们的插件和扩展名 )和本文(虽然有点陈旧,但这是很好的介绍性内容):

http://jfarcand.wordpress.com/2010/11/08/using-jquery-xmpp-and-atmosphere-to-cluster-your-websocketcomet-application/ http://jfarcand.wordpress.com/2010/11/08/using-jquery-xmpp-and-atmosphere-to-cluster-your-websocketcomet-application/

(** If you wanted to use RMI instead, for example, you'd need a JVM on both server and client sides or the RMI-IIOP implementation for CORBA support, which is quite cumbersome for simple tasks. There are also some popular alternatives based on Comet , which is more of a set of techniques than a W3C standard: It's a little more difficult to use than websockets and has a few limitations, but has the benefit of working with legacy technology and implementing real time communication without the need of polling, by using HTTP 1.1 persistent connections) (**例如,如果您想使用RMI,则需要在服务器端和客户端上都使用JVM或RMI-IIOP实现以实现CORBA支持,这对于简单的任务而言非常繁琐。还有一些流行的替代方案基于Comet的 ,它比W3C标准更多的是一套技术:它比websocket难使用,并且有一些局限性,但是具有使用传统技术和实现实时通信而无需使用的好处。通过使用HTTP 1.1持久连接进行轮询)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM