简体   繁体   English

如何在Tapestry5中使用Java套接字?

[英]How to use a java socket in tapestry5?

How do I implement a java socket in tapestry5? 如何在tapestry5中实现Java套接字? What I want to do is create a socket which I can send an XmlHttpRequest over, through a piece of javascript code. 我想要做的是创建一个套接字,我可以通过一段JavaScript代码向其发送XmlHttpRequest。

function sendPost(url, postdata, callback) {

 xmlHttp=GetXmlHttpObject()

 if (xmlHttp==null) {
  alert ("Browser does not support HTTP Request")
  return
 } 

 xmlHttp.onreadystatechange=callback
 xmlHttp.open("POST",url,true)
 xmlHttp.send(postdata);

}

Where the URL is the socket i have just created. URL是我刚创建的套接字。

So you want to do an AJAX request from your client code to the server, recieve a response and process it in some way? 因此,您想从客户端代码向服务器发出AJAX请求,接收响应并以某种方式处理它? You will not need sockets. 您将不需要套接字。 Instead, use Tapestry's built-in AJAX functionality. 而是使用Tapestry的内置AJAX功能。

If you're loading additional content inside your page via Javascript, chances are you will not need to write any code at all. 如果您要通过Javascript在页面内加载其他内容,则很可能根本不需要编写任何代码。 Be sure you have read the AJAX section from the Tapestry docs , and you understand what a Zone is and how it works. 确保已阅读Tapestry文档中AJAX部分 ,并且了解区域是什么以及区域如何工作。

Here's a basic example. 这是一个基本示例。 Template: 模板:

<div id="myZone" t:type="Zone" t:id="myZone">
      ... [Initial content, if any] ...
</div>

<a t:type="ActionLink" t:id="updateContent" t:zone="myZone">Update</a>

And class: 和类:

@Inject 
private Zone myZone;

@Inject
private Request request;

@OnEvent(component = "updateContent")
Object updateContent() {
     ... [your code] ....

     if (this.request.isXHR()) {
         return this.myZone.getBody();
     } else {
         return this;
     }
}

Tapestry will do everything else, like registering the proper event listener on the link and inserting the updated content in the proper place. Tapestry将执行其他所有操作,例如在链接上注册正确的事件侦听器,并将更新的内容插入正确的位置。 The if (this.request.isXHR()) makes sure your page will degrade gracefully for clients without JavaScript enabled. if (this.request.isXHR())确保未启用JavaScript的客户端的页面正常降级

If you'd like to do something else entirely, like returning a JSON object and processing it with your own JavaScript code, you can return any of these JSON classes from your event handler. 如果您想完全做其他事情,例如返回JSON对象并使用自己的JavaScript代码进行处理,则可以从事件处理程序中返回任何这些JSON类

Also, if you want to write your own client-side code, be sure to use the built-in, cross-browser AJAX functionality of Prototype , which ships with Tapestry. 另外,如果要编写自己的客户端代码,请确保使用Tapestry附带的Prototype内置的跨浏览器AJAX功能

Edit based on comment: 根据评论进行编辑:

You won't be able to access a different server (host + port) through AJAX because of the same origin policy . 由于相同的原始策略,您将无法通过AJAX访问其他服务器(主机+端口)。 You could, however, proxy the call through your Tapestry app. 但是,您可以通过Tapestry应用程序代理该呼叫。 I've modified my code to illustrate this (assuming the thing listening on port 2112 is an HTTP server, otherwise change as needed): 我已经修改了代码来说明这一点(假设侦听端口2112的是HTTP服务器,否则可以根据需要进行更改):

@OnEvent(component = "updateContent")
Object updateContent() throws IOException {
     final URL url = new URL("http://localhost:2112");
     final HttpURLConnection con = url.openConnection();

     final String content;

     InputSteam input = null;
     try {
         input = con.getInputStream();
         content = IOUtils.toString(input);
     } finally {
         IOUtils.closeQuietly(input);
     }

     return new StreamResponse() {
         @Override
         public String getContentType() {
             return "text/javascript";
         }

         @Override
         public InputStream getStream() throws IOException {
             return new ByteArrayInputStream(content.getBytes("UTF-8"));
         }

         @Override
         public void prepareResponse(Response response) {
             response.setHeader("Expires", "0");
             response.setHeader("Cache-Control",
                 "must-revalidate, post-check=0, pre-check=0");
         }
     }
}

Use of Sockets is independent of your webapp view framework - you would do it pretty much the same way regardless of how the view is coded. 套接字的使用独立于您的webapp视图框架-不管视图如何编码,您都可以以几乎相同的方式进行操作。 The only thing that changes is once you've implemented your code that uses sockets, is how that's invoked. 唯一改变的是,一旦实现了使用套接字的代码,便是如何调用它。

I used tapestry with spring, and so injecting services into the spring context is the most natural approach. 我在Spring中使用了挂毯,因此将服务注入Spring上下文是最自然的方法。

The services subpackage in tapestry is mostly for creating implementations that plug into tapestry, like encoders, property conduits, and binding factories. Tapestry中的services子包主要用于创建插入Tapestry的实现,例如编码器,属性管道和绑定工厂。 So whether you use this or not depends upon what you are trying to achieve. 因此,是否使用此选项取决于您要实现的目标。

For example, if you are creating a component that reads from a socket, and renders the data read in, then you can create that as a regular component, in the components subpackage. 例如,如果要创建一个从套接字读取并呈现读入数据的组件,则可以在components子包中将其创建为常规组件。

The XmlHttpRequest will just do a web server request which can be handled perfectly well by whatever you use to run Tapestry in. There is no need to open sockets and stuff. XmlHttpRequest只会执行一个Web服务器请求,无论您使用哪种方法来运行Tapestry都可以很好地处理该请求。无需打开套接字和其他东西。

Just define a route in your wep application to accept the XmlHttpRequest and have a handler, servlet, controller, ... collect the necessary data, transform it to xml and send it to the Javascript component. 只需在您的wep应用程序中定义一条路由以接受XmlHttpRequest并拥有一个处理程序,Servlet,控制器...即可收集必要的数据,将其转换为xml并将其发送给Javascript组件。

I found an example here 我在这里找到一个例子

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

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