1515import java .net .URLConnection ;
1616import java .util .Arrays ;
1717import java .util .HashMap ;
18+ import java .util .Iterator ;
1819import java .util .LinkedList ;
1920import java .util .List ;
2021import java .util .Map .Entry ;
3233/**
3334 * The Class IOConnection.
3435 */
35- class IOConnection {
36+ class IOConnection implements IOCallback {
3637 /** Debug logger */
3738 static final Logger logger = Logger .getLogger ("io.socket" );
39+
40+ public static final String FRAME_DELIMITER = "\ufffd " ;
3841
3942 /** The Constant STATE_INIT. */
4043 private static final int STATE_INIT = 0 ;
@@ -93,6 +96,9 @@ class IOConnection {
9396 /** Custom Request headers used while handshaking */
9497 private Properties headers ;
9598
99+ /** Input buffer used for framing */
100+ private final StringBuilder inputBuffer = new StringBuilder ();
101+
96102 /**
97103 * The first socket to be connected. the socket.io server does not send a
98104 * connected response to this one.
@@ -286,8 +292,8 @@ private IOAcknowledge remoteAcknowledge(IOMessage message) {
286292 String _id = message .getId ();
287293 if (_id .equals ("" ))
288294 return null ;
289- else if (_id .endsWith ("+" ) == false )
290- _id = _id + "+" ;
295+ else if (_id .endsWith ("+" ) == false )
296+ _id = _id + "+" ;
291297 final String id = _id ;
292298 final String endPoint = message .getEndpoint ();
293299 return new IOAcknowledge () {
@@ -322,7 +328,7 @@ private void synthesizeAck(IOMessage message, IOAcknowledge ack) {
322328 if (ack != null ) {
323329 int id = nextId ++;
324330 acknowledge .put (id , ack );
325- message .setId (id + "+" );
331+ message .setId (id + "+" );
326332 }
327333 }
328334
@@ -523,11 +529,38 @@ public void transportError(Exception error) {
523529 }
524530
525531 /**
526- * Transport message.
532+ * {@link IOTransport} should call this function if it does not support framing. If it does, transportMessage should be used
527533 *
528534 * @param text
529- * the text {@link IOTransport} calls this, when a message has
530- * been received.
535+ * the text
536+ */
537+ public void transportData (String text ) {
538+ if (!text .startsWith (FRAME_DELIMITER )) {
539+ transportMessage (text );
540+ return ;
541+ }
542+
543+ Iterator <String > fragments = Arrays .asList (text .split (FRAME_DELIMITER )).listIterator (1 );
544+ while (fragments .hasNext ()) {
545+ int length = Integer .parseInt (fragments .next ());
546+ String string = (String ) fragments .next ();
547+ // Potential BUG: it is not defined if length is in bytes or characters. Assuming characters.
548+
549+ if (length != string .length ()) {
550+ error (new SocketIOException ("Garbage from server: " + text ));
551+ return ;
552+ }
553+
554+ transportMessage (string );
555+ }
556+ }
557+
558+ /**
559+ * Transport message. {@link IOTransport} calls this, when a message has
560+ * been received.
561+ *
562+ * @param text
563+ * the text
531564 */
532565 public void transportMessage (String text ) {
533566 logger .info ("< " + text );
@@ -541,21 +574,16 @@ public void transportMessage(String text) {
541574 resetTimeout ();
542575 switch (message .getType ()) {
543576 case IOMessage .TYPE_DISCONNECT :
544- if (message .getEndpoint ().equals ("" )) {
545- for (SocketIO socket : sockets .values ()) {
546- socket .getCallback ().onDisconnect ();
547- }
548- } else
549- try {
550- findCallback (message ).onDisconnect ();
551- } catch (Exception e ) {
552- error (new SocketIOException (
553- "Exception was thrown in onDisconnect()" , e ));
554- }
577+ try {
578+ findCallback (message ).onDisconnect ();
579+ } catch (Exception e ) {
580+ error (new SocketIOException (
581+ "Exception was thrown in onDisconnect()" , e ));
582+ }
555583 break ;
556584 case IOMessage .TYPE_CONNECT :
557585 try {
558- if (firstSocket != null && message .getEndpoint (). equals ( "" )) {
586+ if (firstSocket != null && "" . equals ( message .getEndpoint ())) {
559587 if (firstSocket .getNamespace ().equals ("" )) {
560588 firstSocket .getCallback ().onConnect ();
561589 } else {
@@ -607,7 +635,7 @@ public void transportMessage(String text) {
607635 case IOMessage .TYPE_EVENT :
608636 try {
609637 JSONObject event = new JSONObject (message .getData ());
610- Object [] argsArray = new Object [ 0 ] ;
638+ Object [] argsArray = empty ;
611639 if (event .has ("args" )) {
612640 JSONArray args = event .getJSONArray ("args" );
613641 argsArray = new Object [args .length ()];
@@ -656,14 +684,8 @@ public void transportMessage(String text) {
656684 }
657685 break ;
658686 case IOMessage .TYPE_ERROR :
659- if (message .getEndpoint ().equals ("" ))
660- for (SocketIO socket : sockets .values ()) {
661- socket .getCallback ().onError (
662- new SocketIOException (message .getData ()));
663- }
664- else
665- findCallback (message ).onError (
666- new SocketIOException (message .getData ()));
687+ findCallback (message ).onError (
688+ new SocketIOException (message .getData ()));
667689 if (message .getData ().endsWith ("+0" )) {
668690 // We are advised to disconnect
669691 cleanup ();
@@ -704,9 +726,12 @@ public void reconnect() {
704726 * @return the iO callback
705727 */
706728 private IOCallback findCallback (IOMessage message ) {
729+ if ("" .equals (message .getEndpoint ()))
730+ return this ;
707731 SocketIO socket = sockets .get (message .getEndpoint ());
708732 if (socket == null ) {
709- logger .warning ("Cannot find socket for '" + message .getEndpoint () + "'" );
733+ logger .warning ("Cannot find socket for '" + message .getEndpoint ()
734+ + "'" );
710735 return DUMMY_CALLBACK ;
711736 }
712737 return socket .getCallback ();
@@ -826,35 +851,75 @@ public IOTransport getTransport() {
826851 public void onDisconnect () {
827852 logger .info ("Disconnect" );
828853 }
829-
854+
830855 @ Override
831856 public void onConnect () {
832857 logger .info ("Connect" );
833858 }
834-
859+
835860 @ Override
836861 public void onMessage (String data , IOAcknowledge ack ) {
837862 logger .info ("Message:\n " + data + "\n -------------" );
838863 }
839-
864+
840865 @ Override
841866 public void onMessage (JSONObject json , IOAcknowledge ack ) {
842867 logger .info ("Message:\n " + json .toString () + "\n -------------" );
843868 }
844-
869+
845870 @ Override
846871 public void on (String event , IOAcknowledge ack , Object ... args ) {
847872 logger .info ("Event '" + event + "':\n " );
848873 for (Object arg : args )
849874 logger .info (arg .toString ());
850875 logger .info ("\n -------------" );
851876 }
852-
877+
853878 @ Override
854879 public void onError (SocketIOException socketIOException ) {
855880 logger .info ("Error" );
856881 throw new RuntimeException (socketIOException );
857882 }
858-
883+
859884 };
885+
886+ private static final Object [] empty = new Object [0 ];
887+
888+ @ Override
889+ public void onDisconnect () {
890+ SocketIO socket = sockets .get ("" );
891+ if (socket != null )
892+ socket .getCallback ().onConnect ();
893+ }
894+
895+ @ Override
896+ public void onConnect () {
897+ SocketIO socket = sockets .get ("" );
898+ if (socket != null )
899+ socket .getCallback ().onConnect ();
900+ }
901+
902+ @ Override
903+ public void onMessage (String data , IOAcknowledge ack ) {
904+ for (SocketIO socket : sockets .values ())
905+ socket .getCallback ().onMessage (data , ack );
906+ }
907+
908+ @ Override
909+ public void onMessage (JSONObject json , IOAcknowledge ack ) {
910+ for (SocketIO socket : sockets .values ())
911+ socket .getCallback ().onMessage (json , ack );
912+ }
913+
914+ @ Override
915+ public void on (String event , IOAcknowledge ack , Object ... args ) {
916+ for (SocketIO socket : sockets .values ())
917+ socket .getCallback ().on (event , ack , args );
918+ }
919+
920+ @ Override
921+ public void onError (SocketIOException socketIOException ) {
922+ for (SocketIO socket : sockets .values ())
923+ socket .getCallback ().onError (socketIOException );
924+ }
860925}
0 commit comments