简体   繁体   English

如何用Java触发js事件?

[英]How to trigger js event with Java?

I am building a java chatting web application.(Server and Client in one project) 我正在构建一个java聊天Web应用程序。(一个项目中的服务器和客户端)

follow is my condition 跟着是我的病情

  • Springframework 4.2.3 Springframework 4.2.3
  • JSP JSP
  • Maven Project which converted from a Dynamic Web Project 从动态Web项目转换而来的Maven项目
  • Unable to use node.js 无法使用node.js

So User scenario in my head is 所以我脑子里的用户场景是

  1. User enters some text and press Send 用户输入一些文本并按发送
  2. Ajax call to deliver messages to server Ajax调用将消息传递给服务器
  3. Server checks the users who are currently connected(from Session maybe) 服务器检查当前连接的用户(可能来自Session)
  4. Server calls other users script to append new message 服务器调用其他用户脚本以附加新消息

I am quite confusing with step4. 我对第4步很困惑。 Is it possible that Java calls DOM event trigger? Java是否可能调用DOM事件触发器?

How could a client get a new message event from server? 客户端如何从服务器获取新的消息事件?

Thanks. 谢谢。 :D :d

PS These days majority of chatting servers are event-driven. PS现在大多数聊天服务器都是事件驱动的。 Is it possible to build an event driven chatting server with Java? 是否可以使用Java构建事件驱动的聊天服务器?

Your solution is formed as if there is no WebSocket technology available to you. 您的解决方案就像没有可用的WebSocket技术一样。 WebSocket is implemented to solve real time messaging issues. 实现WebSocket以解决实时消息传递问题。 It pushes message to the destination. 它将消息推送到目的地。

But if to stick wih your method following is meaningful. 但是,如果坚持你的方法以下是有意义的。 You need someplace to keep incoming messages such as database or session.setAttribute [bad idea]. 你需要一个地方来保留传入的消息,如数据库或session.setAttribute [坏主意]。 Then use some ajax call loop on the clients machine to ask for a new incoming message from server. 然后在客户端计算机上使用一些ajax调用循环来请求来自服务器的新传入消息。 Probably your server will slow down due to incoming flood of GET requests from multiple users. 由于来自多个用户的大量GET请求,您的服务器可能会变慢。

to Your last question in post scriptum: yes, I use tomcat websocket api.jar in my projects. 你在post scriptum中的最后一个问题:是的,我在我的项目中使用了tomcat websocket api.jar。 There is well written documentation on apache.org apache.org上有很好的文档

As I mentioned, learn WebSocket if your users are not using old internet explorer browsers. 正如我所提到的,如果您的用户没有使用旧的Internet Explorer浏览器,请学习WebSocket。 There are bunch of tutorials on it... 它上面有很多教程......

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

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