使用Tomcat在Atmosphere框架上的简单应用程序

时间:2013-04-10 06:20:22

标签: tomcat servlets web.xml atmosphere

我对气氛和java很新。我正在尝试使用大气框架创建一个简单的应用程序。

我创建了一个名为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 个答案:

答案 0 :(得分:-1)

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

  2. socket.subscribe(request);
    您没有呼叫连接到服务器。