Skip to content

Commit 8b65876

Browse files
committed
code simplified.
1 parent 81efebe commit 8b65876

File tree

8 files changed

+36
-36
lines changed

8 files changed

+36
-36
lines changed

mobile/src/main/java/io/syslogic/socketio/Constants.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package io.syslogic.socketio;
22

33
public class Constants {
4-
public final static String REQUEST_KEY_LOGIN = "login";
54
public final static String REQUEST_KEY_CHAT_MESSAGE = "chat message";
65
public final static String REQUEST_KEY_DIRECT_MESSAGE = "direct message";
6+
public final static String REQUEST_KEY_USER_LOGIN = "user login";
77
public final static String REQUEST_KEY_USER_ADD = "user add";
88
public final static String REQUEST_KEY_USER_JOINED = "user joined";
99
public final static String REQUEST_KEY_USER_LEFT = "user left";

mobile/src/main/java/io/syslogic/socketio/dialog/SocketsDialogFragment.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
6262
Log.d(LOG_TAG, usercount + " sockets active");
6363

6464
// remove the "sockets" emitter again.
65-
getSocket().off(Constants.REQUEST_KEY_LOGIN, this.onSockets);
65+
getSocket().off(Constants.REQUEST_KEY_USER_LOGIN, this.onSockets);
6666

6767
requireMainActivity().runOnUiThread(() -> {
6868
this.getDataBinding().recyclerviewSockets.setAdapter(new SocketAdapter(items));

mobile/src/main/java/io/syslogic/socketio/fragment/ChatFragment.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class ChatFragment extends BaseFragment implements FragmentResultListener
4747
public void onCreate(Bundle savedInstanceState) {
4848
super.onCreate(savedInstanceState);
4949
MainActivity activity = requireMainActivity();
50-
this.addFragmentResultListener(Constants.REQUEST_KEY_LOGIN, this, true);
50+
this.addFragmentResultListener(Constants.REQUEST_KEY_USER_LOGIN, this, true);
5151
activity.addChatMenuProvider();
5252
if (savedInstanceState == null) {
5353
mSocket = activity.getSocket();

mobile/src/main/java/io/syslogic/socketio/fragment/LoginFragment.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void onCreate(Bundle savedInstanceState) {
4747
mSocket.on(Socket.EVENT_CONNECT, this.onConnect);
4848
mSocket.on(Socket.EVENT_DISCONNECT, this.onDisconnect);
4949
mSocket.on(Socket.EVENT_CONNECT_ERROR, this.onConnectError);
50-
mSocket.on(Constants.REQUEST_KEY_LOGIN, this.onLogin);
50+
mSocket.on(Constants.REQUEST_KEY_USER_LOGIN, this.onLogin);
5151
if (!mSocket.connected()) {mSocket.connect();}
5252
}
5353
}
@@ -86,7 +86,7 @@ public void onDestroy() {
8686
mSocket.off(Socket.EVENT_CONNECT, this.onConnect);
8787
mSocket.off(Socket.EVENT_DISCONNECT, this.onDisconnect);
8888
mSocket.off(Socket.EVENT_CONNECT_ERROR, this.onConnectError);
89-
mSocket.off(Constants.REQUEST_KEY_LOGIN, this.onLogin);
89+
mSocket.off(Constants.REQUEST_KEY_USER_LOGIN, this.onLogin);
9090
super.onDestroy();
9191
}
9292

@@ -112,7 +112,7 @@ private void attemptLogin() {
112112
if (mSocket != null && mSocket.connected()) {
113113

114114
// add the login emitter, before logging in.
115-
mSocket.on(Constants.REQUEST_KEY_LOGIN, this.onLogin);
115+
mSocket.on(Constants.REQUEST_KEY_USER_LOGIN, this.onLogin);
116116
mSocket.emit(Constants.REQUEST_KEY_USER_ADD, username);
117117

118118
} else {
@@ -162,7 +162,7 @@ private void gotoChatFragment(String socketId, String username, int usercount) {
162162
Log.d(LOG_TAG, "room " + socketId + " has " + items.size() + " participants");
163163

164164
// remove the emitter, else it will login endlessly.
165-
mSocket.off(Constants.REQUEST_KEY_LOGIN, this.onLogin);
165+
mSocket.off(Constants.REQUEST_KEY_USER_LOGIN, this.onLogin);
166166

167167
this.gotoChatFragment(socketId, this.username, items.size());
168168

server/modules/constants.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ module.exports = {
22
CONNECTION: "connection",
33
DIRECT_MESSAGE: "direct message",
44
CHAT_MESSAGE: "chat message",
5-
START_TYPING: "typing",
6-
STOP_TYPING: "stop typing",
5+
TYPING_START: "typing start",
6+
TYPING_STOP: "typing stop",
77
DISCONNECT: "disconnect",
8-
USER_LOGIN: "login",
8+
USER_LOGIN: "user login",
99
USER_JOINED: "user joined",
1010
USER_LEFT: "user left",
1111
USER_ADD: "user add",

server/modules/typing.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const constants = require("./constants");
22
module.exports = (io, socket) => {
33

4-
// When the client emits 'typing'
5-
const onStartTyping = () => {
4+
// When the client emits 'typing start'.
5+
const onTypingStart = () => {
66

77
// broadcast globally that the client has started typing
88
socket.broadcast.emit('typing', {
@@ -11,8 +11,8 @@ module.exports = (io, socket) => {
1111
});
1212
}
1313

14-
// When the client emits 'stop typing'
15-
const onStopTyping = () => {
14+
// When the client emits 'typing stop'.
15+
const onTypingStop = () => {
1616

1717
// broadcast globally that the client has stopped typing
1818
socket.broadcast.emit('stop typing', {
@@ -21,6 +21,6 @@ module.exports = (io, socket) => {
2121
});
2222
}
2323

24-
socket.on(constants.START_TYPING, onStartTyping);
25-
socket.on(constants.STOP_TYPING, onStopTyping);
24+
socket.on(constants.TYPING_START, onTypingStart);
25+
socket.on(constants.TYPING_STOP, onTypingStop);
2626
}

server/modules/user.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = (io, socket) => {
1212
return data;
1313
}
1414

15-
// When the client emits 'sockets'
15+
// When the client emits 'sockets'.
1616
const onSockets = () => {
1717
let data = getSockets();
1818
socket.emit(constants.SOCKETS, {
@@ -21,30 +21,30 @@ module.exports = (io, socket) => {
2121
});
2222
};
2323

24-
// When the client connects
25-
const onConnect = (username) => {
24+
// When the client connects.
25+
const onClientConnected = (username) => {
2626

27-
// store the username in the socket session for the client
27+
// Store the username in the socket session for the client.
2828
socket.data.username = username;
2929

3030
socket.emit(constants.USER_LOGIN, {
3131
socketId: socket.id,
3232
data: getSockets()
3333
});
3434

35-
// broadcast globally that the client has joined
35+
// Broadcast globally that the client has joined.
3636
console.log('User %s has connected; socketId %s', socket.data.username, socket.id);
3737
socket.broadcast.emit(constants.USER_JOINED, {
3838
socketId: socket.id,
39-
usercount: io.sockets.sockets.size,
40-
username: socket.data.username
39+
username: socket.data.username,
40+
usercount: io.sockets.sockets.size
4141
});
4242
}
4343

44-
// when the client disconnects
44+
// When the client disconnects.
4545
const onDisconnect = () => {
4646

47-
// broadcast globally that the client has disconnected
47+
// Broadcast globally that the client has disconnected.
4848
console.log('User %s has disconnected; socketId %s', socket.data.username, socket.id);
4949
socket.broadcast.emit(constants.USER_LEFT, {
5050
socketId: socket.id,
@@ -53,7 +53,7 @@ module.exports = (io, socket) => {
5353
});
5454
}
5555

56-
socket.on(constants.SOCKETS, onSockets);
56+
socket.on(constants.USER_ADD, onClientConnected);
5757
socket.on(constants.DISCONNECT, onDisconnect);
58-
socket.on(constants.USER_ADD, onConnect);
58+
socket.on(constants.SOCKETS, onSockets);
5959
}

server/public/main.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,15 @@ $(function() {
179179
if (connected) {
180180
if (!typing) {
181181
typing = true;
182-
socket.emit('typing');
182+
socket.emit('typing start');
183183
}
184184
lastTypingTime = (new Date()).getTime();
185185

186186
setTimeout(() => {
187187
const typingTimer = (new Date()).getTime();
188188
const timeDiff = typingTimer - lastTypingTime;
189189
if (timeDiff >= TYPING_TIMER_LENGTH && typing) {
190-
socket.emit('stop typing');
190+
socket.emit('typing stop');
191191
typing = false;
192192
}
193193
}, TYPING_TIMER_LENGTH);
@@ -224,7 +224,7 @@ $(function() {
224224
if (event.which === 13) {
225225
if (username) {
226226
sendMessage();
227-
socket.emit('stop typing');
227+
socket.emit('typing stop');
228228
typing = false;
229229
} else {
230230
setUsername();
@@ -250,8 +250,8 @@ $(function() {
250250

251251
// Socket events
252252

253-
// Whenever the server emits 'login', log the login message
254-
socket.on('login', (response) => {
253+
// Whenever the server emits 'user login', log the login message
254+
socket.on('user login', (response) => {
255255
connected = true;
256256
// Display the welcome message
257257
// var message = "Welcome to Socket.IO Chat – ";
@@ -290,13 +290,13 @@ $(function() {
290290
removeChatTyping(data);
291291
});
292292

293-
// Whenever the server emits 'typing', show the typing message
294-
socket.on('start typing', (data) => {
293+
// Whenever the server emits 'typing start', show the typing message
294+
socket.on('typing start', (data) => {
295295
addChatTyping(data);
296296
});
297297

298-
// Whenever the server emits 'stop typing', kill the typing message
299-
socket.on('stop typing', (data) => {
298+
// Whenever the server emits 'typing stop', kill the typing message
299+
socket.on('typing stop', (data) => {
300300
removeChatTyping(data);
301301
});
302302

0 commit comments

Comments
 (0)