Skip to content

Commit c28c185

Browse files
committed
Added tests and example for TooTallNate#485
Small fix for WebSocketClient Example for set and get of a attachment Small JUnit test for default value and setter
1 parent 0529363 commit c28c185

File tree

6 files changed

+253
-5
lines changed

6 files changed

+253
-5
lines changed

src/main/example/ChatServer.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,6 @@ public void onMessage( WebSocket conn, ByteBuffer message ) {
7373
}
7474

7575

76-
@Override
77-
public void onFragment( WebSocket conn, Framedata fragment ) {
78-
System.out.println( "received fragment: " + fragment );
79-
}
80-
8176
public static void main( String[] args ) throws InterruptedException , IOException {
8277
WebSocketImpl.DEBUG = true;
8378
int port = 8887; // 843 flash policy port
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright (c) 2010-2017 Nathan Rajlich
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
import org.java_websocket.WebSocket;
27+
import org.java_websocket.WebSocketImpl;
28+
import org.java_websocket.framing.Framedata;
29+
import org.java_websocket.handshake.ClientHandshake;
30+
import org.java_websocket.server.WebSocketServer;
31+
32+
import java.io.BufferedReader;
33+
import java.io.IOException;
34+
import java.io.InputStreamReader;
35+
import java.net.InetSocketAddress;
36+
import java.net.UnknownHostException;
37+
import java.nio.ByteBuffer;
38+
39+
/**
40+
* A simple WebSocketServer implementation. Keeps track of a "chatroom".
41+
*/
42+
public class ChatServerAttachmentExample extends WebSocketServer {
43+
Integer index = 0;
44+
45+
public ChatServerAttachmentExample( int port ) throws UnknownHostException {
46+
super( new InetSocketAddress( port ) );
47+
}
48+
49+
public ChatServerAttachmentExample( InetSocketAddress address ) {
50+
super( address );
51+
}
52+
53+
@Override
54+
public void onOpen( WebSocket conn, ClientHandshake handshake ) {
55+
conn.setAttachment( index );
56+
index++;
57+
System.out.println( conn.getRemoteSocketAddress().getAddress().getHostAddress() + " entered the room! ID: " + conn.<Integer>getAttachment() );
58+
}
59+
60+
@Override
61+
public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
62+
System.out.println( conn + " has left the room! ID: " + conn.<Integer>getAttachment() );
63+
}
64+
65+
@Override
66+
public void onMessage( WebSocket conn, String message ) {
67+
System.out.println( conn + ": " + message );
68+
}
69+
@Override
70+
public void onMessage( WebSocket conn, ByteBuffer message ) {
71+
System.out.println( conn + ": " + message );
72+
}
73+
74+
public static void main( String[] args ) throws InterruptedException , IOException {
75+
WebSocketImpl.DEBUG = true;
76+
int port = 8887; // 843 flash policy port
77+
try {
78+
port = Integer.parseInt( args[ 0 ] );
79+
} catch ( Exception ex ) {
80+
}
81+
ChatServerAttachmentExample s = new ChatServerAttachmentExample( port );
82+
s.start();
83+
System.out.println( "ChatServer started on port: " + s.getPort() );
84+
85+
BufferedReader sysin = new BufferedReader( new InputStreamReader( System.in ) );
86+
while ( true ) {
87+
String in = sysin.readLine();
88+
s.broadcast( in );
89+
if( in.equals( "exit" ) ) {
90+
s.stop(1000);
91+
break;
92+
}
93+
}
94+
}
95+
@Override
96+
public void onError( WebSocket conn, Exception ex ) {
97+
ex.printStackTrace();
98+
if( conn != null ) {
99+
// some errors like port binding failed may not be assignable to a specific websocket
100+
}
101+
}
102+
103+
@Override
104+
public void onStart() {
105+
System.out.println("Server started!");
106+
}
107+
108+
}

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,16 @@ public void send( byte[] data ) throws NotYetConnectedException {
217217
engine.send( data );
218218
}
219219

220+
@Override
221+
public <T> T getAttachment() {
222+
return engine.getAttachment();
223+
}
224+
225+
@Override
226+
public <T> void setAttachment(T attachment) {
227+
engine.setAttachment( attachment );
228+
}
229+
220230
@Override
221231
protected Collection<WebSocket> connections() {
222232
return Collections.singletonList((WebSocket ) engine );

src/test/java/org/java_websocket/AllTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
@RunWith(Suite.class)
3333
@Suite.SuiteClasses({
3434
org.java_websocket.util.ByteBufferUtilsTest.class,
35+
org.java_websocket.client.AllClientTests.class,
3536
org.java_websocket.drafts.AllDraftTests.class,
3637
org.java_websocket.issues.AllIssueTests.class,
3738
org.java_websocket.misc.AllMiscTests.class,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2010-2017 Nathan Rajlich
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package org.java_websocket.client;
27+
28+
29+
import org.junit.runner.RunWith;
30+
import org.junit.runners.Suite;
31+
32+
@RunWith(Suite.class)
33+
@Suite.SuiteClasses({
34+
org.java_websocket.client.AttachmentTest.class
35+
})
36+
/**
37+
* Start all tests for the client
38+
*/
39+
public class AllClientTests {
40+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (c) 2010-2017 Nathan Rajlich
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package org.java_websocket.client;
27+
28+
import org.java_websocket.handshake.ServerHandshake;
29+
import org.junit.Test;
30+
31+
import java.net.URI;
32+
import java.net.URISyntaxException;
33+
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertNull;
36+
37+
public class AttachmentTest {
38+
39+
@Test
40+
public void testDefaultValue() throws URISyntaxException {
41+
WebSocketClient client = new WebSocketClient(new URI( "ws://localhost")) {
42+
@Override
43+
public void onOpen( ServerHandshake handshakedata ) {
44+
45+
}
46+
47+
@Override
48+
public void onMessage( String message ) {
49+
50+
}
51+
52+
@Override
53+
public void onClose( int code, String reason, boolean remote ) {
54+
55+
}
56+
57+
@Override
58+
public void onError( Exception ex ) {
59+
60+
}
61+
};
62+
assertNull(client.getAttachment());
63+
}
64+
65+
@Test
66+
public void testSetter() throws URISyntaxException {
67+
WebSocketClient client = new WebSocketClient(new URI( "ws://localhost")) {
68+
@Override
69+
public void onOpen( ServerHandshake handshakedata ) {
70+
71+
}
72+
73+
@Override
74+
public void onMessage( String message ) {
75+
76+
}
77+
78+
@Override
79+
public void onClose( int code, String reason, boolean remote ) {
80+
81+
}
82+
83+
@Override
84+
public void onError( Exception ex ) {
85+
86+
}
87+
};
88+
assertNull(client.<WebSocketClient>getAttachment());
89+
client.setAttachment( client );
90+
assertEquals( client.<WebSocketClient>getAttachment(), client );
91+
client.setAttachment( null );
92+
assertNull(client.<WebSocketClient>getAttachment());
93+
}
94+
}

0 commit comments

Comments
 (0)