From 10fff38e197140d9966ca81185dc5e8f25983ffb Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 5 Mar 2016 11:15:24 +1300 Subject: Handle some async socket exceptions --- src/org/traccar/api/AsyncSocket.java | 4 ++-- src/org/traccar/api/AsyncSocketServlet.java | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/api/AsyncSocket.java b/src/org/traccar/api/AsyncSocket.java index 7de182d7e..96ad2c450 100644 --- a/src/org/traccar/api/AsyncSocket.java +++ b/src/org/traccar/api/AsyncSocket.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2015 - 2016 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -66,7 +66,7 @@ public class AsyncSocket extends WebSocketAdapter implements ConnectionManager.U } private void sendData(String key, Collection data) { - if (!data.isEmpty()) { + if (!data.isEmpty() && isConnected()) { JsonObjectBuilder json = Json.createObjectBuilder(); json.add(key, JsonConverter.arrayToJson(data)); getRemote().sendString(json.build().toString(), null); diff --git a/src/org/traccar/api/AsyncSocketServlet.java b/src/org/traccar/api/AsyncSocketServlet.java index edfa8939b..ef6cef732 100644 --- a/src/org/traccar/api/AsyncSocketServlet.java +++ b/src/org/traccar/api/AsyncSocketServlet.java @@ -33,8 +33,12 @@ public class AsyncSocketServlet extends WebSocketServlet { factory.setCreator(new WebSocketCreator() { @Override public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) { - long userId = (Long) req.getSession().getAttribute(SessionResource.USER_ID_KEY); - return new AsyncSocket(userId); + if (req.getSession() != null) { + long userId = (Long) req.getSession().getAttribute(SessionResource.USER_ID_KEY); + return new AsyncSocket(userId); + } else { + return null; + } } }); } -- cgit v1.2.3