You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Christoph Mertins <c....@gmail.com> on 2017/10/19 08:22:42 UTC

Tomcat web socket not sending more than one message to the client

Hello,

I have a problem with Tomcat only sending the first message to the client and then no message anymore till the client reconnects.

The code was working for some versions but then stopped working:

	public void handleEvent(EventObject eventObject) {
		Enumeration<String> keySet = userSessions.keys();

		while (keySet.hasMoreElements()) {
			String key = keySet.nextElement();

			Session session = userSessions.get(key);

			synchronized (session) {

				if (session.isOpen()) {
					try {
						Semaphore semaphore = new Semaphore(1);
						Async async = session.getAsyncRemote();
						SendHandler handler = new SemaphoreSendHandler(semaphore, async);

						String text = eventTranscoder.encode(eventObject);

						LogManager.getLogger(this.getClass())
								.info("Eventbus Sending to " + session.getId() + " key: " + key + " text: " + text);

						if (async.getBatchingAllowed()) {
							async.setBatchingAllowed(false);
						}

						semaphore.acquireUninterruptibly();

						async.sendText(text, handler);
						async.flushBatch();

						// session.getBasicRemote().sendObject(eventObject);
					} catch (IllegalStateException | EncodeException | IOException e) {
						LogManager.getLogger(this.getClass()).info("Sending failed", e);
						// userSessions.remove(key);
					}
				} else {
					LogManager.getLogger(this.getClass()).info("Session not open" + key);
					// userSessions.remove(key);
				}
			}
		}
	}

	private class SemaphoreSendHandler implements SendHandler {

		private final Semaphore semaphore;
		private RemoteEndpoint remoteEndpoint;

		private SemaphoreSendHandler(Semaphore semaphore, RemoteEndpoint remoteEndpoint) {
			this.semaphore = semaphore;
			this.remoteEndpoint = remoteEndpoint;
		}

		@Override
		public void onResult(SendResult result) {
			LogManager.getLogger(EventBusEndpoint.class).info("Eventbus Sent ok: " + result.isOK());
			try {
				remoteEndpoint.flushBatch();
				remoteEndpoint.sendPing(ByteBuffer.wrap(new byte[0]));
			} catch (IOException e) {
				LogManager.getLogger(this.getClass()).info("Flushing failed", e);
			}

			semaphore.release();
		}
	}

I can see the SemaphoreSendHandler putting out a true for the result.isOK() call.

But I can’t see any network traffic and no message on the client side. I am checking the with a Chrome Extension (Simple Web Socket Client) on Chrome 61 and 62.

Versions:

Tomcat 8.5.21
Java: Oracle JDK 1.8 151 (tested 131 too)
OS: Raspbian Stretch

I cannot see any reason why this is not working.

Kind Regards,
Christoph
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org