tomcat - 使用 Tomcat 在 Atmosphere 框架上的一个简单应用

标签 tomcat servlets web.xml atmosphere

我对 atmosphere 和 java 很陌生。我正在尝试使用 atmosphere 框架创建一个简单的应用程序。

我创建了一个名为 ChatServlet 的 servlet,它实现了如下所示的 Atmospheerhandler:

 package org.atmosphere.sample;

import java.io.IOException;

import javax.servlet.annotation.WebServlet;

import org.atmosphere.config.service.AtmosphereHandlerService;
import org.atmosphere.cpr.AtmosphereHandler;
import org.atmosphere.cpr.AtmosphereRequest;
import org.atmosphere.cpr.AtmosphereResource;
import org.atmosphere.cpr.AtmosphereResourceEvent;
import org.atmosphere.cpr.AtmosphereResponse;

/**
 * Servlet implementation class ChatServlet
 */
@AtmosphereHandlerService(path="/chat")
public class ChatServlet implements AtmosphereHandler {
    @Override
    public void onRequest(AtmosphereResource r) throws IOException {
        // TODO Auto-generated method stub
        AtmosphereRequest req = r.getRequest();
        if(req.getMethod().equalsIgnoreCase("GET")){
            r.suspend();
        }
        else if (req.getMethod().equalsIgnoreCase("POST")){
            r.getBroadcaster().broadcast(req.getReader().readLine().trim());

        }
    }


    @Override
    public void onStateChange(AtmosphereResourceEvent event) throws IOException {
        // TODO Auto-generated method stub
        AtmosphereResource r=event.getResource();
        AtmosphereResponse res=r.getResponse();

        if(r.isSuspended()){

//           tring message=body.substring("From the server");
            res.getWriter().write("Server Says:Hi");
            switch(r.transport()){
            case JSONP:
            case LONG_POLLING:
                event.getResource().resume();
                break;
            case WEBSOCKET:
            case STREAMING:
                res.getWriter().flush();
                break;
            default:
                break;

            }
            }
        else if(!event.isResuming()){
            event.broadcaster().broadcast("Say bye bye");
        }
    }


    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }


}

这是一个 html 文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Atmosphere Chat</title>
<script src="jquery-1.9.1.js"></script>
<script src="application.js"></script>
<script src="jquery.atmosphere.js"></script>
</head>

    <style>
        * {
            font-family: tahoma;
            font-size: 12px;
            padding: 0px;
            margin: 0px;
        }

        p {
            line-height: 18px;
        }

        div {
            width: 500px;
            margin-left: auto;
            margin-right: auto;
        }

        #content {
            padding: 5px;
            background: #ddd;
            border-radius: 5px;
            border: 1px solid #CCC;
            margin-top: 10px;
        }

        #header {
            padding: 5px;
            background: #f5deb3;
            border-radius: 5px;
            border: 1px solid #CCC;
            margin-top: 10px;
        }

        #input {
            border-radius: 2px;
            border: 1px solid #ccc;
            margin-top: 10px;
            padding: 5px;
            width: 400px;
        }

        #status {
            width: 88px;
            display: block;
            float: left;
            margin-top: 15px;
        }
    </style>
</head>
<body>
<div id="header"><h3>Atmosphere Chat. Default transport is WebSocket, fallback is long-polling</h3></div>
<div id="content"></div>
<div>
    <span id="status">Connecting...</span>
    <input type="text" id="input" disabled="disabled"/>
</div>
</body>
</html>

这是我的js文件:

var socket=$.atmopshere;
debugger;
var request ={
                url:'http://localhost:8080/Reverse-Ajax/' + 'chat',
                 contentType : "application/json",
                 transport :'websocket',
                fallbackTransport:'long-polling'};

request.onOpen=function(response){
//  content.html($('<p>',{text:'Atmosphere connected uisng'+ response.transport}));
//  input.removeAttr('disabled').focus();
//  status.text('Choose name:');
    console.log("onopen",response);

};

request.onMessage=function (response){
    var message=response.responseBody;
    console.log(message);

};

下面显示的是我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:j2ee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2.5.xsd" version="2.5">
  <description>Atmosphere Chat</description>
  <display-name>Atmosphere Chat</display-name>
  <servlet>
    <description>AtmosphereServlet</description>
    <servlet-name>AtmosphereServlet</servlet-name>
    <servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
    <async-supported>true</async-supported>
  </servlet>
  <servlet-mapping>
    <servlet-name>AtmosphereServlet</servlet-name>
    <url-pattern>/chat/*</url-pattern>
  </servlet-mapping>
</web-app>

我无法连接到服务器。有人可以建议我哪里出错了。 我使用 tomcat 7 作为我的服务器。 提前致谢。

最佳答案

  1. 错误:var socket=$.atmopshere;
    右:var socket=$.atmosphere;

  2. socket.subscribe(request);:
    您没有调用连接到服务器。

关于tomcat - 使用 Tomcat 在 Atmosphere 框架上的一个简单应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15918406/

相关文章:

java - 我如何处理多个服务器 MongoDB

java - 难以将 SSL 证书安装到 Tomcat 中

java - 坚不可摧 - HTTP 参数

javascript - 从 select onchange 调用 servlet 并传递值

java - URL 模式不适用于 localhost url(没有 maven 的 jersey)

java - 类型不匹配 : cannot convert from Boolean to boolean

servlets - java.lang.IllegalStateException:提交响应后无法(转发| sendRedirect |创建 session )

java - Spring MVC - 从 web.xml 注入(inject)上下文参数

java - Jersey web.xml 到基于注释的配置以及 HK2 绑定(bind)

java - 使用来自共享类加载器的 EHCache 时出现 ClassCastException