这是我的WebSocket
端点
import javax.websocket.CloseReason;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.twitter.hbc.core.Client;
@ServerEndpoint("/tweets")
public class TweetStreamServer {
private static final Logger LOGGER = LoggerFactory.getLogger(TweetStreamServer.class);
@OnMessage
public void tweets(final String message, final Session client) throws IOException, InterruptedException {
LOGGER.debug("registering search term {}", message);
final TwitterHoseBird twitterHoseBird = new TwitterHoseBird();
final Client twitterClient = twitterHoseBird.getInstance(message);
while(!twitterClient.isDone()) {
client.getAsyncRemote().sendText(twitterHoseBird.getMsgQueue().take());
}
}
@OnClose
public void onClose(CloseReason reason) {
System.out.println("Closing connection");
LOGGER.warn("closing connection {}", reason);
}
}
当我部署它时,我尝试通过JavaScript
代码
var connection = new WebSocket('ws://127.0.0.1:8080/tweetstream-1.0-SNAPSHOT/tweets');
connection.onmessage = function (e) {
console.log('Server: ' + e.data);
};
connection.send('fifa');
这开始从服务器发送推文。现在,当我关闭客户端的连接时,我会
connection.close();
问题吗
我想在websocket客户端连接关闭后关闭与twitter的连接。我希望执行以下代码
@OnClose
public void onClose(CloseReason reason) {
System.out.println("Closing connection");
LOGGER.warn("closing connection {}", reason);
}
但在服务器日志中,我没有看到任何此类声明
/Users/harith/code/installers/wildfly-8.1.0.Final/bin/standalone.sh
=========================================================================
/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/bin/java -classpath "/Applications/IntelliJ IDEA 13.app/lib/idea_rt.jar:/Applications/IntelliJ IDEA 13.app/lib/util.jar" -Dfile.encoding=UTF-8 com.intellij.rt.execution.CommandLineWrapper /private/var/folders/qs/y62p93xs0bdb9ptk6l2r8vw0002kvk/T/classpath571663656928489966.tmp com.intellij.javaee.oss.process.JavaeeProcess 53801 com.intellij.javaee.oss.jboss.agent.JBoss71Agent
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
JBoss Bootstrap Environment
[2014-07-12 02:16:06,305] Artifact tweetstream:war: Server is not connected. Deploy is not available.
JBOSS_HOME: /Users/harith/code/installers/wildfly-8.1.0.Final
Detected server admin port: 9990
Detected server http port: 8080
JAVA: /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/bin/java
JAVA_OPTS: -server -Xms64m -Xmx512m -XX:MaxPermSize=256m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true
=========================================================================
[0m14:16:06,048 INFO [org.jboss.modules] (main) JBoss Modules version 1.3.3.Final
[0m[0m14:16:06,289 INFO [org.jboss.msc] (main) JBoss MSC version 1.2.2.Final
[0m[0m14:16:06,349 INFO [org.jboss.as] (MSC service thread 1-6) JBAS015899: WildFly 8.1.0.Final "Kenny" starting
[0m[0m14:16:07,165 INFO [org.jboss.as.server] (Controller Boot Thread) JBAS015888: Creating http management service using socket-binding (management-http)
[0m[0m14:16:07,180 INFO [org.xnio] (MSC service thread 1-8) XNIO version 3.2.2.Final
[0m[0m14:16:07,186 INFO [org.xnio.nio] (MSC service thread 1-8) XNIO NIO Implementation Version 3.2.2.Final
[0m[0m14:16:07,207 INFO [org.jboss.as.security] (ServerService Thread Pool -- 45) JBAS013171: Activating Security Subsystem
[0m[33m14:16:07,208 WARN [org.jboss.as.txn] (ServerService Thread Pool -- 46) JBAS010153: Node identifier property is set to the default value. Please make sure it is unique.
[0m[0m14:16:07,211 INFO [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 32) JBAS010280: Activating Infinispan subsystem.
[0m[0m14:16:07,213 INFO [org.wildfly.extension.io] (ServerService Thread Pool -- 31) WFLYIO001: Worker 'default' has auto-configured to 16 core threads with 128 task threads based on your 8 available processors
[0m[0m14:16:07,219 INFO [org.jboss.as.naming] (ServerService Thread Pool -- 40) JBAS011800: Activating Naming Subsystem
[0m[0m14:16:07,221 INFO [org.jboss.as.webservices] (ServerService Thread Pool -- 48) JBAS015537: Activating WebServices Extension
[0m[0m14:16:07,222 INFO [org.jboss.as.security] (MSC service thread 1-13) JBAS013170: Current PicketBox version=4.0.21.Beta1
[0m[0m14:16:07,222 INFO [org.jboss.as.jsf] (ServerService Thread Pool -- 38) JBAS012615: Activated the following JSF Implementations: [main]
[0m[0m14:16:07,251 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 47) JBAS017502: Undertow 1.0.15.Final starting
[0m[0m14:16:07,252 INFO [org.wildfly.extension.undertow] (MSC service thread 1-3) JBAS017502: Undertow 1.0.15.Final starting
[0m[0m14:16:07,266 INFO [org.jboss.as.connector.logging] (MSC service thread 1-11) JBAS010408: Starting JCA Subsystem (IronJacamar 1.1.5.Final)
[0m[0m14:16:07,291 INFO [org.jboss.remoting] (MSC service thread 1-8) JBoss Remoting version 4.0.3.Final
[0m[0m14:16:07,291 INFO [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 27) JBAS010403: Deploying JDBC-compliant driver class org.h2.Driver (version 1.3)
[0m[0m14:16:07,299 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-5) JBAS010417: Started Driver service with driver-name = h2
[0m[0m14:16:07,346 INFO [org.jboss.as.naming] (MSC service thread 1-15) JBAS011802: Starting Naming Service
[0m[0m14:16:07,346 INFO [org.jboss.as.mail.extension] (MSC service thread 1-13) JBAS015400: Bound mail session [java:jboss/mail/Default]
[0m[0m14:16:07,390 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 47) JBAS017527: Creating file handler for path /Users/harith/code/installers/wildfly-8.1.0.Final/welcome-content
[0m[0m14:16:07,425 INFO [org.wildfly.extension.undertow] (MSC service thread 1-1) JBAS017525: Started server default-server.
[0m[0m14:16:07,498 INFO [org.wildfly.extension.undertow] (MSC service thread 1-13) JBAS017531: Host default-host starting
[0m[0m14:16:07,561 INFO [org.wildfly.extension.undertow] (MSC service thread 1-2) JBAS017519: Undertow HTTP listener default listening on /127.0.0.1:8080
[0m[0m14:16:07,724 INFO [org.jboss.as.server.deployment.scanner] (MSC service thread 1-13) JBAS015012: Started FileSystemDeploymentService for directory /Users/harith/code/installers/wildfly-8.1.0.Final/standalone/deployments
[0m[0m14:16:07,728 INFO [org.jboss.as.server.deployment] (MSC service thread 1-3) JBAS015876: Starting deployment of "4f0c2559-5f61-4cf1-b441-c6288b419b80.war" (runtime-name: "4f0c2559-5f61-4cf1-b441-c6288b419b80.war")
[0m[0m14:16:07,736 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-6) JBAS010400: Bound data source [java:jboss/datasources/ExampleDS]
[0m[0m14:16:07,918 INFO [org.jboss.ws.common.management] (MSC service thread 1-8) JBWS022052: Starting JBoss Web Services - Stack CXF Server 4.2.4.Final
[0m[0m14:16:08,062 INFO [io.undertow.websockets.jsr] (MSC service thread 1-11) UT026003: Adding annotated server endpoint class com.self.tweetstream.TweetStreamServer for path /tweets
[0m[0m14:16:08,075 INFO [io.undertow.websockets.jsr] (MSC service thread 1-11) UT026004: Adding annotated client endpoint class com.self.tweetstream.TweetStreamClient
[0m[0m14:16:08,167 INFO [org.wildfly.extension.undertow] (MSC service thread 1-11) JBAS017534: Registered web context: /4f0c2559-5f61-4cf1-b441-c6288b419b80
[0m[0m14:16:08,203 INFO [org.jboss.as.server] (Controller Boot Thread) JBAS018559: Deployed "4f0c2559-5f61-4cf1-b441-c6288b419b80.war" (runtime-name : "4f0c2559-5f61-4cf1-b441-c6288b419b80.war")
[0m[0m14:16:08,215 INFO [org.jboss.as] (Controller Boot Thread) JBAS015961: Http management interface listening on http://127.0.0.1:9990/management
[0m[0m14:16:08,215 INFO [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://127.0.0.1:9990
[0m[0m14:16:08,216 INFO [org.jboss.as] (Controller Boot Thread) JBAS015874: WildFly 8.1.0.Final "Kenny" started in 2554ms - Started 250 of 304 services (91 services are lazy, passive or on-demand)
[0mConnected to server
[2014-07-12 02:16:08,683] Artifact tweetstream:war: Artifact is being deployed, please wait...
[0m14:16:08,794 INFO [org.jboss.as.server.deployment] (MSC service thread 1-4) JBAS015876: Starting deployment of "tweetstream-1.0-SNAPSHOT.war" (runtime-name: "tweetstream-1.0-SNAPSHOT.war")
[0m[0m14:16:09,408 INFO [org.jboss.weld.deployer] (MSC service thread 1-5) JBAS016002: Processing weld deployment tweetstream-1.0-SNAPSHOT.war
[0m[0m14:16:09,450 INFO [org.hibernate.validator.internal.util.Version] (MSC service thread 1-5) HV000001: Hibernate Validator 5.1.0.Final
[0m[0m14:16:09,548 INFO [org.jboss.weld.deployer] (MSC service thread 1-14) JBAS016005: Starting Services for CDI deployment: tweetstream-1.0-SNAPSHOT.war
[0m[0m14:16:09,573 INFO [org.jboss.weld.Version] (MSC service thread 1-14) WELD-000900: 2.1.2 (Final)
[0m[0m14:16:09,580 INFO [org.jboss.weld.deployer] (MSC service thread 1-1) JBAS016008: Starting weld service for deployment tweetstream-1.0-SNAPSHOT.war
[0m[0m14:16:10,361 INFO [io.undertow.websockets.jsr] (MSC service thread 1-8) UT026004: Adding annotated client endpoint class com.self.tweetstream.TweetStreamClient
[0m[0m14:16:10,364 INFO [io.undertow.websockets.jsr] (MSC service thread 1-8) UT026003: Adding annotated server endpoint class com.self.tweetstream.TweetStreamServer for path /tweets
[0m[0m14:16:10,383 INFO [javax.enterprise.resource.webcontainer.jsf.config] (MSC service thread 1-8) Initializing Mojarra 2.2.6-jbossorg-4 20140501-1134 for context '/tweetstream-1.0-SNAPSHOT'
[0m[0m14:16:11,293 INFO [org.wildfly.extension.undertow] (MSC service thread 1-8) JBAS017534: Registered web context: /tweetstream-1.0-SNAPSHOT
[0m[0m14:16:11,307 INFO [org.jboss.as.server] (management-handler-thread - 2) JBAS018559: Deployed "tweetstream-1.0-SNAPSHOT.war" (runtime-name : "tweetstream-1.0-SNAPSHOT.war")
[0m[2014-07-12 02:16:11,327] Artifact tweetstream:war: Artifact is deployed successfully
[2014-07-12 02:16:11,327] Artifact tweetstream:war: Deploy took 2,644 milliseconds
[0m14:16:21,124 INFO [com.twitter.hbc.httpclient.BasicClient] (default task-2) New connection executed: tweetStream-client, endpoint: /1.1/statuses/filter.json?delimited=length&stall_warnings=true
[0m[0m14:16:21,207 INFO [com.twitter.hbc.httpclient.ClientBase] (hosebird-client-io-thread-0) tweetStream-client Establishing a connection
[0m[0m14:16:23,789 INFO [com.twitter.hbc.httpclient.ClientBase] (hosebird-client-io-thread-0) tweetStream-client Processing connection data
[0m
我在这里缺少什么?
更新
我测试如下
@Test
public void test() throws URISyntaxException, IOException, DeploymentException, InterruptedException {
System.out.println("URI: " + getEndpointUrl());
TweetStreamClient.latch = new CountDownLatch(1);
Session session = connectToServer(TweetStreamClient.class, "tweets");
assertNotNull(session);
// (todo: harit) assert correct things
// assertTrue(TweetStreamClient.latch.await(10, TimeUnit.SECONDS));
// assertEquals("Hello", TweetStreamClient.response);
}
当我运行此测试时,我会在日志中看到以下内容
8295 [main] INFO io.undertow.websockets.jsr - UT026004: Adding annotated client endpoint class com.self.tweetstream.TweetStreamClient
14:59:26,981 INFO [org.wildfly.extension.undertow] (MSC service thread 1-13) JBAS017535: Unregistered web context: /b5b7b561-8b49-4c69-b55f-7859b86da36d
14:59:26,983 INFO [stdout] (default task-2) session id:AMAjTU1TYTCjXBgLOKnMXS6q, search term: Hello World!
14:59:26,985 INFO [stdout] (default task-2) Closing session: io.undertow.websockets.jsr.UndertowSession@cd7ecfa
14:59:26,986 WARN [com.self.tweetstream.TweetStreamServer] (default task-2) closing session: AMAjTU1TYTCjXBgLOKnMXS6q, reason: CloseReason[1001]
但是当我从JavaScript调用connection.close()
时,我看不到这样的事情
答案 0 :(得分:1)
我遇到了同样的问题,最初认为这是一个Wildfly问题,但后来注意到Firefox正在触发@OnClose。当我添加@OnError时,Chromium正在触发它。
我发现another StackOverflow question指的是相同的行为。
一个月前,Chromium似乎只有fixed this issue。