Skip to content

Commit 91f1fad

Browse files
committed
Adding a second basic example.
1 parent e199b1e commit 91f1fad

2 files changed

Lines changed: 83 additions & 25 deletions

File tree

‎README.markdown‎

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,49 +49,46 @@ Using socket.io-java-client is quite simple. But lets see:
4949

5050
``` java
5151

52-
// Initialise a socket:
53-
SocketIO socket = new IOSocket("http://127.0.0.1:3001");
54-
socket.connect(new IOCallback() {
52+
SocketIO socket = new SocketIO("http://127.0.0.1:3001/");
53+
socket.connect(new IOCallback() {
5554
@Override
5655
public void onMessage(JSONObject json, IOAcknowledge ack) {
57-
System.out.println("We received a message: " + json.toString(2));
56+
try {
57+
System.out.println("Server said:" + json.toString(2));
58+
} catch (JSONException e) {
59+
e.printStackTrace();
60+
}
5861
}
59-
62+
6063
@Override
61-
public void onMessage(String data, IOAcknowleged ack) {
62-
System.out.println("We received a message:" + data);
64+
public void onMessage(String data, IOAcknowledge ack) {
65+
System.out.println("Server said: " + data);
6366
}
64-
67+
6568
@Override
6669
public void onError(SocketIOException socketIOException) {
67-
System.out.println("Something went wrong. Lets exit");
68-
System.exit(0);
70+
System.out.println("an Error occured");
71+
socketIOException.printStackTrace();
6972
}
70-
73+
7174
@Override
7275
public void onDisconnect() {
73-
System.out.println("Disconnected");
74-
System.exit(0);
76+
System.out.println("Connection terminated.");
7577
}
76-
78+
7779
@Override
7880
public void onConnect() {
79-
System.out.println("Connected");
81+
System.out.println("Connection established");
8082
}
81-
83+
8284
@Override
8385
public void on(String event, IOAcknowledge ack, Object... args) {
84-
try {
85-
ack.ack("Roger that!");
86-
socket.emit("answer", new JSONObject().put("msg", "Hello again Socket.io!"));
87-
} catch (JSONException e) {
88-
e.printStackTrace();
89-
}
86+
System.out.println("Server triggered event '" + event + "'");
9087
}
9188
});
92-
93-
// This will be cached until the server is connected.
94-
socket.emit("hello", new JSONObject().put("msg", "Hello Socket.io! :D"));
89+
90+
socket.send("Hello Server!");
91+
9592
```
9693

9794
For further informations, read the [Javadoc](http://s01.de/~tox/hgexport/socket.io-java-client/).

‎examples/BasicExample2.java‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.net.MalformedURLException;
2+
3+
import org.json.JSONException;
4+
import org.json.JSONObject;
5+
6+
import io.socket.IOAcknowledge;
7+
import io.socket.IOCallback;
8+
import io.socket.SocketIO;
9+
import io.socket.SocketIOException;
10+
11+
public class BasicExample2 {
12+
public static void main(String[] args) {
13+
try {
14+
new BasicExample2();
15+
} catch (Exception e) {
16+
e.printStackTrace();
17+
}
18+
}
19+
20+
public BasicExample2() throws Exception {
21+
SocketIO socket = new SocketIO("http://127.0.0.1:3001/");
22+
socket.connect(new IOCallback() {
23+
@Override
24+
public void onMessage(JSONObject json, IOAcknowledge ack) {
25+
try {
26+
System.out.println("Server said:" + json.toString(2));
27+
} catch (JSONException e) {
28+
e.printStackTrace();
29+
}
30+
}
31+
32+
@Override
33+
public void onMessage(String data, IOAcknowledge ack) {
34+
System.out.println("Server said: " + data);
35+
}
36+
37+
@Override
38+
public void onError(SocketIOException socketIOException) {
39+
System.out.println("an Error occured");
40+
socketIOException.printStackTrace();
41+
}
42+
43+
@Override
44+
public void onDisconnect() {
45+
System.out.println("Connection terminated.");
46+
}
47+
48+
@Override
49+
public void onConnect() {
50+
System.out.println("Connection established");
51+
}
52+
53+
@Override
54+
public void on(String event, IOAcknowledge ack, Object... args) {
55+
System.out.println("Server triggered event '" + event + "'");
56+
}
57+
});
58+
59+
socket.send("Hello Server!");
60+
}
61+
}

0 commit comments

Comments
 (0)