Removing old WebSocket interface API and fixing build failures as result

This commit is contained in:
Joakim Erdfelt 2012-06-28 15:30:29 -07:00
parent d2561acde4
commit f56b2acd8d
6 changed files with 143 additions and 494 deletions

View File

@ -1,273 +0,0 @@
/*******************************************************************************
* Copyright (c) 2011 Mort Bay Consulting Pty. Ltd.
* ======================================================================
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*******************************************************************************/
package org.eclipse.jetty.websocket;
import java.io.IOException;
/**
* WebSocket Interface.
* <p>
* This interface provides the signature for a server-side end point of a websocket connection.
* The Interface has several nested interfaces, for each type of message that may be received.
*/
public interface WebSocket
{
/**
* Called when a new websocket connection is accepted.
* @param connection The Connection object to use to send messages.
*/
void onOpen(Connection connection);
/**
* Called when an established websocket connection closes
* @param closeCode
* @param message
*/
void onClose(int closeCode, String message);
/**
* A nested WebSocket interface for receiving text messages
*/
interface OnTextMessage extends WebSocket
{
/**
* Called with a complete text message when all fragments have been received.
* The maximum size of text message that may be aggregated from multiple frames is set with {@link Connection#setMaxTextMessageSize(int)}.
* @param data The message
*/
void onMessage(String data);
}
/**
* A nested WebSocket interface for receiving binary messages
*/
interface OnBinaryMessage extends WebSocket
{
/**
* Called with a complete binary message when all fragments have been received.
* The maximum size of binary message that may be aggregated from multiple frames is set with {@link Connection#setMaxBinaryMessageSize(int)}.
* @param data
* @param offset
* @param length
*/
void onMessage(byte[] data, int offset, int length);
}
/**
* A nested WebSocket interface for receiving control messages
*/
interface OnControl extends WebSocket
{
/**
* Called when a control message has been received.
* @param controlCode
* @param data
* @param offset
* @param length
* @return true if this call has completely handled the control message and no further processing is needed.
*/
boolean onControl(byte controlCode,byte[] data, int offset, int length);
}
/**
* A nested WebSocket interface for receiving any websocket frame
*/
interface OnFrame extends WebSocket
{
/**
* Called when any websocket frame is received.
* @param flags
* @param opcode
* @param data
* @param offset
* @param length
* @return true if this call has completely handled the frame and no further processing is needed (including aggregation and/or message delivery)
*/
boolean onFrame(byte flags,byte opcode,byte[] data, int offset, int length);
void onHandshake(FrameConnection connection);
}
/**
* A Connection interface is passed to a WebSocket instance via the {@link WebSocket#onOpen(Connection)} to
* give the application access to the specifics of the current connection. This includes methods
* for sending frames and messages as well as methods for interpreting the flags and opcodes of the connection.
*/
public interface Connection
{
String getProtocol();
void sendMessage(String data) throws IOException;
void sendMessage(byte[] data, int offset, int length) throws IOException;
/**
* @deprecated Use {@link #close()}
*/
void disconnect();
/**
* Close the connection with normal close code.
*/
void close();
/** Close the connection with specific closeCode and message.
* @param closeCode The close code to send, or -1 for no close code
* @param message The message to send or null for no message
*/
void close(int closeCode,String message);
boolean isOpen();
/**
* @param ms The time in ms that the connection can be idle before closing
*/
void setMaxIdleTime(int ms);
/**
* @param size size<0 No aggregation of frames to messages, >=0 max size of text frame aggregation buffer in characters
*/
void setMaxTextMessageSize(int size);
/**
* @param size size<0 no aggregation of binary frames, >=0 size of binary frame aggregation buffer
*/
void setMaxBinaryMessageSize(int size);
/**
* @return The time in ms that the connection can be idle before closing
*/
int getMaxIdleTime();
/**
* Size in characters of the maximum text message to be received
* @return size <0 No aggregation of frames to messages, >=0 max size of text frame aggregation buffer in characters
*/
int getMaxTextMessageSize();
/**
* Size in bytes of the maximum binary message to be received
* @return size <0 no aggregation of binary frames, >=0 size of binary frame aggregation buffer
*/
int getMaxBinaryMessageSize();
}
/**
* Frame Level Connection
* <p>The Connection interface at the level of sending/receiving frames rather than messages.
* Also contains methods to decode/generate flags and opcodes without using constants, so that
* code can be written to work with multiple drafts of the protocol.
*
*/
public interface FrameConnection extends Connection
{
/**
* @return The opcode of a binary message
*/
byte binaryOpcode();
/**
* @return The opcode of a text message
*/
byte textOpcode();
/**
* @return The opcode of a continuation frame
*/
byte continuationOpcode();
/**
* @return Mask for the FIN bit.
*/
byte finMask();
/** Set if frames larger than the frame buffer are handled with local fragmentations
* @param allowFragmentation
*/
void setAllowFrameFragmentation(boolean allowFragmentation);
/**
* @param flags The flags bytes of a frame
* @return True of the flags indicate a final frame.
*/
boolean isMessageComplete(byte flags);
/**
* @param opcode
* @return True if the opcode is for a control frame
*/
boolean isControl(byte opcode);
/**
* @param opcode
* @return True if the opcode is for a text frame
*/
boolean isText(byte opcode);
/**
* @param opcode
* @return True if the opcode is for a binary frame
*/
boolean isBinary(byte opcode);
/**
* @param opcode
* @return True if the opcode is for a continuation frame
*/
boolean isContinuation(byte opcode);
/**
* @param opcode
* @return True if the opcode is a close control
*/
boolean isClose(byte opcode);
/**
* @param opcode
* @return True if the opcode is a ping control
*/
boolean isPing(byte opcode);
/**
* @param opcode
* @return True if the opcode is a pong control
*/
boolean isPong(byte opcode);
/**
* @return True if frames larger than the frame buffer are fragmented.
*/
boolean isAllowFrameFragmentation();
/** Send a control frame
* @param control
* @param data
* @param offset
* @param length
* @throws IOException
*/
void sendControl(byte control,byte[] data, int offset, int length) throws IOException;
/** Send an arbitrary frame
* @param flags
* @param opcode
* @param data
* @param offset
* @param length
* @throws IOException
*/
void sendFrame(byte flags,byte opcode,byte[] data, int offset, int length) throws IOException;
}
}

View File

@ -37,7 +37,6 @@ import org.eclipse.jetty.server.SelectChannelConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.api.WebSocketBehavior;
import org.eclipse.jetty.websocket.api.WebSocketException;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
@ -45,42 +44,13 @@ import org.eclipse.jetty.websocket.frames.BaseFrame;
import org.eclipse.jetty.websocket.frames.TextFrame;
import org.eclipse.jetty.websocket.generator.Generator;
import org.eclipse.jetty.websocket.parser.Parser;
import org.eclipse.jetty.websocket.server.examples.MyEchoSocket;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class WebSocketLoadRFC6455Test
{
private static class EchoWebSocket implements WebSocket.OnTextMessage
{
private volatile Connection outbound;
@Override
public void onClose(int closeCode, String message)
{
}
@Override
public void onMessage(String data)
{
try
{
// System.err.println(">> "+data);
outbound.sendMessage(data);
}
catch (IOException x)
{
outbound.disconnect();
}
}
@Override
public void onOpen(Connection outbound)
{
this.outbound = outbound;
}
}
private class WebSocketClient implements Runnable
{
private final Socket socket;
@ -198,7 +168,7 @@ public class WebSocketLoadRFC6455Test
threadPool.setMaxStopTimeMs(1000);
_server.setThreadPool(threadPool);
WebSocketHandler wsHandler = new WebSocketHandler.Simple(EchoWebSocket.class);
WebSocketHandler wsHandler = new WebSocketHandler.Simple(MyEchoSocket.class);
wsHandler.setHandler(new DefaultHandler());
_server.setHandler(wsHandler);

View File

@ -37,10 +37,20 @@ import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.Utf8StringBuilder;
import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.annotations.OnWebSocketBinary;
import org.eclipse.jetty.websocket.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.annotations.OnWebSocketFrame;
import org.eclipse.jetty.websocket.annotations.OnWebSocketText;
import org.eclipse.jetty.websocket.annotations.WebSocket;
import org.eclipse.jetty.websocket.api.AcceptHash;
import org.eclipse.jetty.websocket.api.OpCode;
import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.frames.BaseFrame;
import org.eclipse.jetty.websocket.frames.CloseFrame;
import org.eclipse.jetty.websocket.frames.PingFrame;
import org.eclipse.jetty.websocket.frames.PongFrame;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
@ -48,7 +58,8 @@ import org.junit.Test;
public class WebSocketMessageRFC6455Test
{
private static class TestWebSocket implements WebSocket.OnFrame, WebSocket.OnBinaryMessage, WebSocket.OnTextMessage
@WebSocket
public static class TestWebSocket
{
protected boolean _latch;
boolean _onConnect = false;
@ -56,7 +67,7 @@ public class WebSocketMessageRFC6455Test
boolean _aggregate = false;
private final CountDownLatch connected = new CountDownLatch(1);
private final CountDownLatch disconnected = new CountDownLatch(1);
private volatile FrameConnection connection;
private WebSocketConnection connection;
private boolean awaitConnected(long time) throws InterruptedException
{
@ -68,58 +79,40 @@ public class WebSocketMessageRFC6455Test
return disconnected.await(time,TimeUnit.MILLISECONDS);
}
public FrameConnection getConnection()
{
return connection;
}
@Override
@OnWebSocketClose
public void onClose(int code, String message)
{
disconnected.countDown();
}
@Override
public boolean onFrame(byte flags, byte opcode, byte[] data, int offset, int length)
@OnWebSocketFrame
public boolean onFrame(BaseFrame frame)
{
if (_echo)
{
OpCode op = OpCode.from(opcode);
switch (op)
if (!(frame instanceof PingFrame) && !(frame instanceof PongFrame) && !(frame instanceof CloseFrame))
{
case CLOSE:
case PING:
case PONG:
break;
default:
try
{
connection.sendFrame(flags,opcode,data,offset,length);
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
connection.write(frame);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return false;
}
@Override
public void onHandshake(FrameConnection connection)
{
this.connection = connection;
}
@Override
@OnWebSocketBinary
public void onMessage(byte[] data, int offset, int length)
{
if (_aggregate)
{
try
{
connection.sendMessage(data,offset,length);
connection.write(data,offset,length);
}
catch (IOException e)
{
@ -128,7 +121,7 @@ public class WebSocketMessageRFC6455Test
}
}
@Override
@OnWebSocketText
public void onMessage(String data)
{
__textCount.incrementAndGet();
@ -148,7 +141,7 @@ public class WebSocketMessageRFC6455Test
{
try
{
connection.sendMessage(data);
connection.write(data);
}
catch (IOException e)
{
@ -157,14 +150,15 @@ public class WebSocketMessageRFC6455Test
}
}
@Override
public void onOpen(Connection connection)
@OnWebSocketConnect
public void onOpen(WebSocketConnection connection)
{
this.connection = connection;
if (_onConnect)
{
try
{
connection.sendMessage("sent on connect");
connection.write("sent on connect");
}
catch (IOException e)
{
@ -307,7 +301,6 @@ public class WebSocketMessageRFC6455Test
assertTrue(__serverWebSocket.awaitConnected(1000));
assertNotNull(__serverWebSocket.connection);
__serverWebSocket.getConnection().setMaxBinaryMessageSize(1024);
output.write(OpCode.BINARY.getCode());
output.write(0x8a);
@ -377,7 +370,6 @@ public class WebSocketMessageRFC6455Test
assertTrue(__serverWebSocket.awaitConnected(1000));
assertNotNull(__serverWebSocket.connection);
__serverWebSocket.connection.setMaxIdleTime(60000);
// Send and receive 1 message
output.write(mesg);
@ -462,7 +454,6 @@ public class WebSocketMessageRFC6455Test
assertTrue(__serverWebSocket.awaitConnected(1000));
assertNotNull(__serverWebSocket.connection);
__serverWebSocket.connection.setMaxIdleTime(60000);
__latch.countDown();
// wait 2s and then consume messages
@ -499,7 +490,7 @@ public class WebSocketMessageRFC6455Test
String mesg = "How Now Brown Cow";
for (int i = 0; i < count; i++)
{
__serverWebSocket.connection.sendMessage(mesg);
__serverWebSocket.connection.write(mesg);
if ((i % 100) == 0)
{
output.flush();
@ -658,7 +649,7 @@ public class WebSocketMessageRFC6455Test
assertTrue(__serverWebSocket.awaitConnected(1000));
assertNotNull(__serverWebSocket.connection);
__serverWebSocket.getConnection().close(tests[t][0],mesg[t]);
__serverWebSocket.connection.close(tests[t][0],mesg[t]);
byte[] buf = new byte[128];
int len = input.read(buf);
@ -921,12 +912,12 @@ public class WebSocketMessageRFC6455Test
assertTrue(__serverWebSocket.awaitDisconnected(5000));
try
{
__serverWebSocket.connection.sendMessage("Don't send");
assertTrue(false);
__serverWebSocket.connection.write("Don't send");
Assert.fail("Should have thrown IOException");
}
catch (IOException e)
{
assertTrue(true);
Assert.assertThat("IOException",e.getMessage(),containsString("TODO"));
}
}
@ -951,8 +942,6 @@ public class WebSocketMessageRFC6455Test
assertTrue(__serverWebSocket.awaitConnected(1000));
assertNotNull(__serverWebSocket.connection);
__serverWebSocket.getConnection().setMaxBinaryMessageSize(15);
output.write(0x02);
output.write(0x8a);
output.write(0xff);
@ -1006,8 +995,6 @@ public class WebSocketMessageRFC6455Test
assertTrue(__serverWebSocket.awaitConnected(1000));
assertNotNull(__serverWebSocket.connection);
__serverWebSocket.getConnection().setMaxBinaryMessageSize(15);
output.write(0x02);
output.write(0x94);
output.write(0xff);
@ -1049,8 +1036,6 @@ public class WebSocketMessageRFC6455Test
assertTrue(__serverWebSocket.awaitConnected(1000));
assertNotNull(__serverWebSocket.connection);
__serverWebSocket.getConnection().setMaxTextMessageSize(15);
output.write(0x01);
output.write(0x8a);
output.write(0xff);
@ -1104,8 +1089,6 @@ public class WebSocketMessageRFC6455Test
assertTrue(__serverWebSocket.awaitConnected(1000));
assertNotNull(__serverWebSocket.connection);
__serverWebSocket.getConnection().setMaxTextMessageSize(15);
output.write(0x01);
output.write(0x94);
output.write(0xff);
@ -1147,9 +1130,6 @@ public class WebSocketMessageRFC6455Test
assertTrue(__serverWebSocket.awaitConnected(1000));
assertNotNull(__serverWebSocket.connection);
__serverWebSocket.getConnection().setMaxTextMessageSize(10 * 1024);
__serverWebSocket.getConnection().setAllowFrameFragmentation(true);
output.write(0x81);
output.write(0x80 | 0x7E);
output.write((byte)((16 * 1024) >> 8));
@ -1193,8 +1173,6 @@ public class WebSocketMessageRFC6455Test
assertTrue(__serverWebSocket.awaitConnected(1000));
assertNotNull(__serverWebSocket.connection);
__serverWebSocket.getConnection().setMaxBinaryMessageSize(15);
output.write(0x81);
output.write(0x82);
output.write(0x00);
@ -1315,7 +1293,7 @@ public class WebSocketMessageRFC6455Test
message.append(text);
}
String data = message.toString();
__serverWebSocket.connection.sendMessage(data);
__serverWebSocket.connection.write(data);
assertEquals(OpCode.TEXT.getCode(),input.read());
assertEquals(0x7e,input.read());
@ -1387,12 +1365,12 @@ public class WebSocketMessageRFC6455Test
try
{
__serverWebSocket.connection.sendMessage("Don't send");
assertTrue(false);
__serverWebSocket.connection.write("Don't send");
Assert.fail("Should have thrown IOException");
}
catch (IOException e)
{
assertTrue(true);
Assert.assertThat("IOException",e.getMessage(),containsString("TODO"));
}
}

View File

@ -25,8 +25,9 @@ import org.eclipse.jetty.server.ssl.SslSelectChannelConnector;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.annotations.MyEchoSocket;
import org.eclipse.jetty.websocket.api.WebSocketAdapter;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
@ -37,7 +38,7 @@ public class WebSocketOverSSLTest
private int _port;
private QueuedThreadPool _threadPool;
// private WebSocketClientFactory _wsFactory;
private WebSocket.Connection _connection;
private WebSocketConnection _connection;
@After
public void destroy() throws Exception
@ -62,7 +63,7 @@ public class WebSocketOverSSLTest
}
}
private void startClient(final WebSocket webSocket) throws Exception
private void startClient(final Object webSocket) throws Exception
{
Assert.assertTrue(_server.isStarted());
@ -81,7 +82,7 @@ public class WebSocketOverSSLTest
// _connection = client.open(new URI("wss://localhost:" + _port), webSocket).get(5, TimeUnit.SECONDS);
}
private void startServer(final Class<?> websocketPojo) throws Exception
private void startServer(final Object websocket) throws Exception
{
_server = new Server();
SslSelectChannelConnector connector = new SslSelectChannelConnector();
@ -90,7 +91,7 @@ public class WebSocketOverSSLTest
cf.setKeyStorePath(MavenTestingUtils.getTestResourceFile("keystore").getAbsolutePath());
cf.setKeyStorePassword("storepwd");
cf.setKeyManagerPassword("keypwd");
_server.setHandler(new WebSocketHandler.Simple(websocketPojo));
_server.setHandler(new WebSocketHandler.Simple(websocket.getClass()));
_server.start();
_port = connector.getLocalPort();
}
@ -101,23 +102,13 @@ public class WebSocketOverSSLTest
startServer(MyEchoSocket.class);
int count = 1000;
final CountDownLatch clientLatch = new CountDownLatch(count);
startClient(new WebSocket.OnTextMessage()
startClient(new WebSocketAdapter()
{
@Override
public void onClose(int closeCode, String message)
{
}
@Override
public void onMessage(String data)
public void onWebSocketText(String message)
{
clientLatch.countDown();
}
@Override
public void onOpen(Connection connection)
{
}
});
char[] chars = new char[256];
@ -125,7 +116,7 @@ public class WebSocketOverSSLTest
String message = new String(chars);
for (int i = 0; i < count; ++i)
{
_connection.sendMessage(message);
_connection.write(message);
}
Assert.assertTrue(clientLatch.await(20,TimeUnit.SECONDS));
@ -138,61 +129,46 @@ public class WebSocketOverSSLTest
@Test
public void testWebSocketOverSSL() throws Exception
{
// final String message = "message";
// final CountDownLatch serverLatch = new CountDownLatch(1);
// startServer(new WebSocket.OnTextMessage()
// {
// private Connection connection;
//
// @Override
// public void onClose(int closeCode, String message)
// {
// }
//
// @Override
// public void onMessage(String data)
// {
// try
// {
// Assert.assertEquals(message,data);
// connection.sendMessage(data);
// serverLatch.countDown();
// }
// catch (IOException x)
// {
// x.printStackTrace();
// }
// }
//
// @Override
// public void onOpen(Connection connection)
// {
// this.connection = connection;
// }
// });
// final CountDownLatch clientLatch = new CountDownLatch(1);
// startClient(new WebSocket.OnTextMessage()
// {
// @Override
// public void onClose(int closeCode, String message)
// {
// }
//
// @Override
// public void onMessage(String data)
// {
// Assert.assertEquals(message,data);
// clientLatch.countDown();
// }
//
// @Override
// public void onOpen(Connection connection)
// {
// }
// });
// _connection.sendMessage(message);
//
// Assert.assertTrue(serverLatch.await(5,TimeUnit.SECONDS));
// Assert.assertTrue(clientLatch.await(5,TimeUnit.SECONDS));
final String message = "message";
final CountDownLatch serverLatch = new CountDownLatch(1);
startServer(new WebSocketAdapter()
{
private WebSocketConnection connection;
@Override
public void onWebSocketConnect(WebSocketConnection connection)
{
this.connection = connection;
}
@Override
public void onWebSocketText(String message)
{
try
{
Assert.assertEquals(message,message);
connection.write(message);
serverLatch.countDown();
}
catch (IOException x)
{
x.printStackTrace();
}
}
});
final CountDownLatch clientLatch = new CountDownLatch(1);
startClient(new WebSocketAdapter()
{
@Override
public void onWebSocketText(String data)
{
Assert.assertEquals(message,data);
clientLatch.countDown();
}
});
_connection.write(message);
Assert.assertTrue(serverLatch.await(5,TimeUnit.SECONDS));
Assert.assertTrue(clientLatch.await(5,TimeUnit.SECONDS));
}
}

View File

@ -15,14 +15,15 @@
*******************************************************************************/
package org.eclipse.jetty.websocket.server.helper;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.api.WebSocketAdapter;
public class CaptureSocket implements WebSocket.OnTextMessage
public class CaptureSocket extends WebSocketAdapter
{
private final CountDownLatch latch = new CountDownLatch(1);
public List<String> messages;
@ -34,13 +35,11 @@ public class CaptureSocket implements WebSocket.OnTextMessage
public boolean awaitConnected(long timeout) throws InterruptedException
{
return latch.await(timeout, TimeUnit.MILLISECONDS);
return latch.await(timeout,TimeUnit.MILLISECONDS);
}
public void onMessage(String data)
public void onClose(int closeCode, String message)
{
// System.out.printf("Received Message \"%s\" [size %d]%n", data, data.length());
messages.add(data);
}
public void onOpen(Connection connection)
@ -48,7 +47,10 @@ public class CaptureSocket implements WebSocket.OnTextMessage
latch.countDown();
}
public void onClose(int closeCode, String message)
@Override
public void onWebSocketText(String message)
{
// System.out.printf("Received Message \"%s\" [size %d]%n", message, message.length());
messages.add(message);
}
}

View File

@ -19,52 +19,16 @@ import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.api.WebSocketAdapter;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
public class MessageSender implements WebSocket
public class MessageSender extends WebSocketAdapter
{
private Connection conn;
private CountDownLatch connectLatch = new CountDownLatch(1);
private int closeCode = -1;
private String closeMessage = null;
public void onOpen(Connection connection)
{
this.conn = connection;
connectLatch.countDown();
}
public void onClose(int closeCode, String message)
{
this.conn = null;
this.closeCode = closeCode;
this.closeMessage = message;
}
public boolean isConnected()
{
if (this.conn == null)
{
return false;
}
return this.conn.isOpen();
}
public int getCloseCode()
{
return closeCode;
}
public String getCloseMessage()
{
return closeMessage;
}
public void sendMessage(String format, Object... args) throws IOException
{
this.conn.sendMessage(String.format(format,args));
}
public void awaitConnect() throws InterruptedException
{
connectLatch.await(1,TimeUnit.SECONDS);
@ -72,10 +36,42 @@ public class MessageSender implements WebSocket
public void close()
{
if (this.conn == null)
try
{
return;
getConnection().close(StatusCode.NORMAL,null);
}
this.conn.close();
catch (IOException e)
{
e.printStackTrace();
}
}
public int getCloseCode()
{
return closeCode;
}
public String getCloseMessage()
{
return closeMessage;
}
@Override
public void onWebSocketClose(int statusCode, String reason)
{
this.closeCode = statusCode;
this.closeMessage = reason;
}
@Override
public void onWebSocketConnect(WebSocketConnection connection)
{
super.onWebSocketConnect(connection);
connectLatch.countDown();
}
public void sendMessage(String format, Object... args) throws IOException
{
getConnection().write(String.format(format,args));
}
}