已建立大气套接字,但没有接收/发送数据
Posted
技术标签:
【中文标题】已建立大气套接字,但没有接收/发送数据【英文标题】:Atmosphere socket established but no data being received/sent 【发布时间】:2012-10-30 19:26:54 【问题描述】:我正在关注 Atmosphere tutorial 以获得基本的聊天应用程序设置。目前在运行 javascript 时,它似乎能够使用 Web 套接字建立与服务器的连接。我在 Chrome 控制台中得到以下信息:
Invoking executeWebSocket core.js:2298
Using URL: ws://localhost:8080/web-transport/chat?X-Atmosphere-tracking-id=0&X-Atmosphere-Framework=1.1&X-Atmosphere-Transport=websocket&X-Cache-Date=0&Content-Type=application/json core.js:2298
Websocket successfully opened
但是,当我尝试发布数据 (subSocket.push(...)) 时,似乎什么也没发生。我曾尝试调试 AtmosphereHandlerService,但它似乎从未被击中(并且没有生成任何日志。
我错过了什么会导致这种情况?
到目前为止,我有以下内容:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:j2ee="http://java.sun.com/xml/ns/javaee" 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.5.xsd">
<description>AtmosphereServlet</description>
<display-name>AtmosphereServlet</display-name>
<servlet>
<description>AtmosphereServlet</description>
<servlet-name>AtmosphereServlet</servlet-name>
<servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
<!-- If you want to use Servlet 3.0 -->
<!-- <async-supported>true</async-supported> -->
<!-- List of init-param -->
</servlet>
<servlet-mapping>
<servlet-name>AtmosphereServlet</servlet-name>
<!-- Any mapping -->
<url-pattern>/chat/*</url-pattern>
</servlet-mapping>
</web-app>
ChatAtmosphereHandler.java
@AtmosphereHandlerService(path = "/chat")
public class ChatAtmosphereHandler implements AtmosphereHandler
public void onRequest(AtmosphereResource resource) throws IOException
AtmosphereRequest request = resource.getRequest();
if (request.getMethod().equalsIgnoreCase("GET"))
resource.suspend();
else if (request.getMethod().equalsIgnoreCase("POST"))
resource.getBroadcaster().broadcast(request.getReader().readLine().trim());
public void onStateChange(AtmosphereResourceEvent event) throws IOException
AtmosphereResource resource = event.getResource();
AtmosphereResponse response = resource.getResponse();
if (resource.isSuspended())
response.getWriter().write("Hello");
switch (resource.transport())
case JSONP:
case LONG_POLLING:
event.getResource().resume();
break;
case WEBSOCKET:
case STREAMING:
response.getWriter().flush();
break;
else if (!event.isResuming())
event.broadcaster().broadcast("bye bye");
public void destroy()
Maven 依赖项:
<dependency>
<groupId>org.atmosphere</groupId>
<artifactId>atmosphere-runtime</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.atmosphere</groupId>
<artifactId>atmosphere-jersey</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>eu.infomas</groupId>
<artifactId>annotation-detector</artifactId>
<version>3.0.1</version>
</dependency>
最后是javascript:
$(document).ready(function()
var socket = $.atmosphere;
var subSocket = null;
var request =
url: 'http://localhost:8080/web-transport/' + 'chat',
contentType : "application/json",
logLevel : 'debug',
transport : 'websocket' ,
fallbackTransport: 'long-polling'
;
request.onOpen = function(response)
console.log('onopen', response);
;
request.onReconnect = function (request, response)
console.log('onreconnect', request, response);
;
request.onMessage = function (response)
console.log('onmessage', response);
;
request.onError = function(response)
console.log('onerror', response);
;
$('#send').click(function()
subSocket.push(JSON.stringify( author: 'me', message: 'hello' ));
);
$('#subscribe').click(function()
subSocket = socket.subscribe(request);
);
);
【问题讨论】:
【参考方案1】:我终于发现问题不在于代码或氛围,而在于 Glassfish。
解决方案是使用以下命令启用 Web 套接字:
asadmin set configs.config.server-config.network-config.protocols.protocol.http-listener-1.http.websockets-support-enabled=true
确保使用命令提示符运行命令。我之前通过管理 gui 启用了 Web 套接字,但不知何故,它似乎没有正确应用。在我运行上述命令并尝试上述代码后,它运行良好。
【讨论】:
以上是关于已建立大气套接字,但没有接收/发送数据的主要内容,如果未能解决你的问题,请参考以下文章
本地连接已连接上,但数据包异常,多是发送多接收少,无法上网,求解??
GCDAsyncUDPSocket:虽然发送成功,但没有收到任何数据
socket 错误之:OSError: [WinError 10057] 由于套接字没有连接并且(当使用一个 sendto 调用发送数据报套接字时)没有提供地址,发送或接收数据的请求没有被接受。