gpt4 book ai didi

java - 使用 java 和 Atmosphere 的基本聊天应用程序

转载 作者:行者123 更新时间:2023-12-04 05:27:35 25 4
gpt4 key购买 nike

我想得到一个简单的example与 Glassfish 和 Jersery 合作的 Atmosphere 聊天程序。

到目前为止,我有一个基本的 maven webapp 原型(prototype),其结构如下:

project setup

聊天资源文件只是一个使用 Atmosphere 注释的简单 REST 服务:

package core;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.atmosphere.annotation.Broadcast;
import org.atmosphere.annotation.Suspend;

@Path("/")
public class ChatResource {

@Suspend(contentType = "application/json")
@GET
public String suspend() {
return "";
}

@Broadcast(writeEntity = false)
@POST
@Produces("application/json")
public Response broadcast(Message message) {
return new Response(message.author, message.message);
}
}

消息和响应类只是简单的 POJO 包装器。 index.jsp、application.js、jquery.atmosphere.js 和 jquery.js 直接从示例项目中复制而来(找到 here)。

我的 web.xml 设置为以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:j2ee="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Archetype Created Web Application</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>
</servlet>
<servlet-mapping>
<servlet-name>AtmosphereServlet</servlet-name>
<url-pattern>/chat/*</url-pattern>
</servlet-mapping>
</web-app>

导航到该页面时,我收到以下错误:

Sorry, but there's some problem with your socket or the server is down



如果我导航到页面尝试访问的路径 ( http://localhost:8080/.../chat),我会收到以下消息:

org.atmosphere.cpr.AtmosphereMappingException: No AtmosphereHandler found. Make sure you define it inside META-INF/atmosphere.xml or annotate using @AtmosphereHandlerService



我一定是缺少文件或设置配置,但我似乎找不到在哪里(上面的消息说 Atmosphere .xml 丢失,但这没有出现在示例中)。任何帮助,将不胜感激。

最佳答案

默认情况下,Glassfish 中未启用 Web 套接字协议(protocol)。

您可以使用以下命令启用它:

asadmin set configs.config.server-config.network-config.protocols.protocol.http-listener-1.http.websockets-support-enabled=true

这是我需要让我的连接正常工作。

关于java - 使用 java 和 Atmosphere 的基本聊天应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13018717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com