1- /*
2- * socket.io-java-client WebsocketTransport.java
3- *
4- * Copyright (c) 2012, Enno Boland
5- * socket.io-java-client is a implementation of the socket.io protocol in Java.
6- *
7- * See LICENSE file for more information
8- */
91package io .socket ;
102
11-
123import java .io .IOException ;
134import java .net .URI ;
145import java .net .URL ;
156import java .util .regex .Pattern ;
167
17- import de .roderick .weberknecht .WebSocketConnection ;
18- import de .roderick .weberknecht .WebSocketEventHandler ;
19- import de .roderick .weberknecht .WebSocketException ;
20- import de .roderick .weberknecht .WebSocketMessage ;
21-
22- /**
23- * The Class WebsocketTransport.
24- */
25- class WebsocketTransport implements IOTransport , WebSocketEventHandler {
26-
27- WebSocketConnection websocket ;
28-
29- /** Pattern used to replace http:// by ws:// respectively https:// by wss:// */
30- private final static Pattern PATTERN_HTTP = Pattern .compile ("^http" );
31-
32- /** The String to identify this Transport */
33- public static final String TRANSPORT_NAME = "websocket" ;
34-
35- /** The IOConnection of this transport. */
36- private IOConnection connection ;
37-
38- /**
39- * Creates a new Transport for the given url an {@link IOConnection}.
40- *
41- * @param url the url
42- * @param connection the connection
43- * @return the iO transport
44- */
45- public static IOTransport create (URL url , IOConnection connection ) {
46- URI uri = URI .create (
47- PATTERN_HTTP .matcher (url .toString ()).replaceFirst ("ws" )
48- + IOConnection .SOCKET_IO_1 + TRANSPORT_NAME
49- + "/" + connection .getSessionId ());
50-
51- return new WebsocketTransport (uri , connection );
52- }
53-
54- /**
55- * Instantiates a new websocket transport.
56- *
57- * @param uri the uri
58- * @param connection the connection
59- * @throws WebSocketException
60- */
61- public WebsocketTransport (URI uri , IOConnection connection ) {
62- try {
63- websocket = new WebSocketConnection (uri );
64- } catch (WebSocketException e ) {
65- connection .transportError (e );
66- return ;
67- }
68- this .connection = connection ;
69- websocket .setEventHandler (this );
70- }
71-
72- /* (non-Javadoc)
73- * @see io.socket.IOTransport#disconnect()
74- */
75- @ Override
76- public void disconnect () {
77- try {
78- websocket .close ();
79- } catch (Exception e ) {
80- connection .transportError (e );
81- }
82- }
83-
84- /* (non-Javadoc)
85- * @see io.socket.IOTransport#canSendBulk()
86- */
87- @ Override
88- public boolean canSendBulk () {
89- return false ;
90- }
91-
92- /* (non-Javadoc)
93- * @see io.socket.IOTransport#sendBulk(java.lang.String[])
94- */
95- @ Override
96- public void sendBulk (String [] texts ) throws IOException {
97- throw new RuntimeException ("Cannot send Bulk!" );
98- }
99-
100- /* (non-Javadoc)
101- * @see io.socket.IOTransport#invalidate()
102- */
103- @ Override
104- public void invalidate () {
105- connection = null ;
106- }
107-
108- @ Override
109- public void onClose () {
110- if (connection != null )
111- connection .transportDisconnected ();
112- }
113-
114- @ Override
115- public void onMessage (WebSocketMessage arg0 ) {
116- if (connection != null )
117- connection .transportMessage (arg0 .getText ());
118- }
119-
120- @ Override
121- public void onOpen () {
122- if (connection != null )
123- connection .transportConnected ();
124- }
125-
126- @ Override
127- public void connect () {
128- try {
129- websocket .connect ();
130- } catch (Exception e ) {
131- connection .transportError (e );
132- }
133- }
134-
135- @ Override
136- public void send (String text ) throws Exception {
137- websocket .send (text );
138- }
139-
140- @ Override
141- public String getName () {
142- return TRANSPORT_NAME ;
143- }
144- }
8+ import javax .net .ssl .SSLContext ;
9+ import javax .net .ssl .SSLSocketFactory ;
10+
11+ import org .java_websocket .client .DefaultSSLWebSocketClientFactory ;
12+ import org .java_websocket .client .WebSocketClient ;
13+ import org .java_websocket .handshake .ServerHandshake ;
14+
15+ class WebsocketTransport extends WebSocketClient implements IOTransport {
16+ private final static Pattern PATTERN_HTTP = Pattern .compile ("^http" );
17+ public static final String TRANSPORT_NAME = "websocket" ;
18+ private IOConnection connection ;
19+ public static IOTransport create (URL url , IOConnection connection ) {
20+ URI uri = URI .create (
21+ PATTERN_HTTP .matcher (url .toString ()).replaceFirst ("ws" )
22+ + IOConnection .SOCKET_IO_1 + TRANSPORT_NAME
23+ + "/" + connection .getSessionId ());
24+
25+ return new WebsocketTransport (uri , connection );
26+ }
27+
28+ public WebsocketTransport (URI uri , IOConnection connection ) {
29+ super (uri );
30+ this .connection = connection ;
31+ SSLContext context = IOConnection .getSslContext ();
32+ if ("wss" .equals (uri .getScheme ()) && context != null ) {
33+ this .setWebSocketFactory (new DefaultSSLWebSocketClientFactory (context ));
34+ }
35+ }
36+
37+ /* (non-Javadoc)
38+ * @see io.socket.IOTransport#disconnect()
39+ */
40+ @ Override
41+ public void disconnect () {
42+ try {
43+ this .close ();
44+ } catch (Exception e ) {
45+ connection .transportError (e );
46+ }
47+ }
48+
49+ /* (non-Javadoc)
50+ * @see io.socket.IOTransport#canSendBulk()
51+ */
52+ @ Override
53+ public boolean canSendBulk () {
54+ return false ;
55+ }
56+
57+ /* (non-Javadoc)
58+ * @see io.socket.IOTransport#sendBulk(java.lang.String[])
59+ */
60+ @ Override
61+ public void sendBulk (String [] texts ) throws IOException {
62+ throw new RuntimeException ("Cannot send Bulk!" );
63+ }
64+
65+ /* (non-Javadoc)
66+ * @see io.socket.IOTransport#invalidate()
67+ */
68+ @ Override
69+ public void invalidate () {
70+ connection = null ;
71+ }
72+
73+ @ Override
74+ public void onClose (int code , String reason , boolean remote ) {
75+ if (connection != null )
76+ connection .transportDisconnected ();
77+ }
78+
79+ @ Override
80+ public void onMessage (String text ) {
81+ if (connection != null )
82+ connection .transportMessage (text );
83+ }
84+
85+ @ Override
86+ public void onOpen (ServerHandshake handshakedata ) {
87+ if (connection != null )
88+ connection .transportConnected ();
89+ }
90+
91+ @ Override
92+ public String getName () {
93+ return TRANSPORT_NAME ;
94+ }
95+
96+ @ Override
97+ public void onError (Exception ex ) {
98+ // TODO Auto-generated method stub
99+
100+ }
101+ }
0 commit comments