Skip to content

Modified IOWebSocket to comply with the new WebSocketClient interface. #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions examples/chat-client/src/org/socketio/chat/Chat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.socketio.chat;

import com.clwillingham.socket.io.IOSocket;
import java.io.IOException;
import org.json.JSONException;
import org.json.JSONObject;

public class Chat extends Thread {
private IOSocket socket;
private ChatCallback callback;

public Chat(ChatCallbackAdapter callback) {
this.callback = new ChatCallback(callback);
}

@Override
public void run() {
socket = new IOSocket("http://localhost:3000", callback);
socket.connect();
}

public void sendMessage(String message) {
try {
JSONObject json = new JSONObject();
json.putOpt("message", message);
socket.emit("user message", json);
} catch (IOException ex) {
ex.printStackTrace();
} catch (JSONException ex) {
ex.printStackTrace();
}
}

public void join(String nickname) {
try {
JSONObject json = new JSONObject();
json.putOpt("nickname", nickname);
socket.emit("nickname", json, callback);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
51 changes: 51 additions & 0 deletions examples/chat-client/src/org/socketio/chat/ChatCallback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.socketio.chat;

import com.clwillingham.socket.io.AckCallback;
import com.clwillingham.socket.io.MessageCallback;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ChatCallback extends AckCallback implements MessageCallback {
private ChatCallbackAdapter callback;

public ChatCallback(ChatCallbackAdapter callback) {
this.callback = callback;
}

@Override
public void callback(JSONArray data) throws JSONException {
callback.callback(data);
}

@Override
public void on(String event, JSONObject... data) {
callback.on(event, data);
}

@Override
public void onMessage(String message) {
callback.onMessage(message);
}

@Override
public void onMessage(JSONObject json) {
callback.onMessage(json);
}

@Override
public void onConnect() {
callback.onConnect();
}

@Override
public void onDisconnect() {
callback.onDisconnect();
}

@Override
public void onConnectFailure() {
callback.onConnectFailure();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.socketio.chat;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public interface ChatCallbackAdapter {
public void callback(JSONArray data) throws JSONException;
public void on(String event, JSONObject... data);
public void onMessage(String message);
public void onMessage(JSONObject json);
public void onConnect();
public void onDisconnect();
public void onConnectFailure();
}
107 changes: 107 additions & 0 deletions examples/chat-client/src/org/socketio/chat/ChatFrame.form
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?xml version="1.1" encoding="UTF-8" ?>

<Form version="1.3" maxVersion="1.7" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
<Properties>
<Property name="defaultCloseOperation" type="int" value="3"/>
<Property name="title" type="java.lang.String" value="Chat"/>
</Properties>
<SyntheticProperties>
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
</SyntheticProperties>
<AuxValues>
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
<AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
</AuxValues>

<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="1" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="1" attributes="0">
<Component id="MessagesScrollPane" alignment="0" pref="376" max="32767" attributes="0"/>
<Group type="102" alignment="1" attributes="0">
<Component id="NewMessageScrollPane" min="-2" pref="295" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="SendButton" pref="75" max="32767" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<Component id="OnlineUsersLabel" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="OnlineUsers" pref="326" max="32767" attributes="0"/>
</Group>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="1" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="OnlineUsersLabel" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="OnlineUsers" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="MessagesScrollPane" pref="192" max="32767" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" max="-2" attributes="0">
<Component id="SendButton" max="32767" attributes="1"/>
<Component id="NewMessageScrollPane" alignment="0" pref="51" max="32767" attributes="1"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Container class="javax.swing.JScrollPane" name="MessagesScrollPane">

<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
<SubComponents>
<Component class="javax.swing.JTextArea" name="MessagesTextArea">
<Properties>
<Property name="columns" type="int" value="20"/>
<Property name="editable" type="boolean" value="false"/>
<Property name="rows" type="int" value="5"/>
</Properties>
</Component>
</SubComponents>
</Container>
<Container class="javax.swing.JScrollPane" name="NewMessageScrollPane">

<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
<SubComponents>
<Component class="javax.swing.JTextArea" name="NewMessageTextArea">
<Properties>
<Property name="columns" type="int" value="20"/>
<Property name="rows" type="int" value="5"/>
</Properties>
</Component>
</SubComponents>
</Container>
<Component class="javax.swing.JButton" name="SendButton">
<Properties>
<Property name="text" type="java.lang.String" value="Send"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="SendButtonActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JLabel" name="OnlineUsersLabel">
<Properties>
<Property name="text" type="java.lang.String" value="Online:"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="OnlineUsers">
</Component>
</SubComponents>
</Form>
Loading