Skip to content

Commit 4e203b6

Browse files
committed
emitter for private messages.
1 parent f44a66a commit 4e203b6

File tree

4 files changed

+43
-36
lines changed

4 files changed

+43
-36
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@ lint/tmp/
5353
/server/.idea/php.xml
5454
/server/.idea/modules.xml
5555
/server/.idea/misc.xml
56+
/server/.idea/jsLibraryMappings.xml

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

+22-16
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@ public class ChatFragment extends BaseFragment {
4040
private final Handler mTypingHandler = new Handler(Looper.getMainLooper());
4141
private boolean mTyping = false;
4242

43-
@Override
44-
public void onAttach(@NonNull Context context) {
45-
super.onAttach(context);
46-
mAdapter = new ChatAdapter(this.mItems);
47-
}
48-
4943
@Override
5044
public void onCreate(Bundle savedInstanceState) {
5145
super.onCreate(savedInstanceState);
@@ -56,11 +50,12 @@ public void onCreate(Bundle savedInstanceState) {
5650
mSocket.on(Socket.EVENT_CONNECT, this.onConnect);
5751
mSocket.on(Socket.EVENT_DISCONNECT, this.onDisconnect);
5852
mSocket.on(Socket.EVENT_CONNECT_ERROR, this.onConnectError);
59-
mSocket.on("new message", onNewMessage);
60-
mSocket.on("user joined", onUserJoined);
61-
mSocket.on("user left", onUserLeft);
62-
mSocket.on("typing", onTyping);
63-
mSocket.on("stop typing", onStopTyping);
53+
mSocket.on("new message", this.onNewMessage);
54+
mSocket.on("private message", this.onPrivateMessage);
55+
mSocket.on("user joined", this.onUserJoined);
56+
mSocket.on("user left", this.onUserLeft);
57+
mSocket.on("typing", this.onTyping);
58+
mSocket.on("stop typing", this.onStopTyping);
6459
if (!mSocket.connected()) {mSocket.connect();}
6560
}
6661
}
@@ -71,17 +66,24 @@ public void onDestroy() {
7166
mSocket.off(Socket.EVENT_CONNECT, this.onConnect);
7267
mSocket.off(Socket.EVENT_DISCONNECT, this.onDisconnect);
7368
mSocket.off(Socket.EVENT_CONNECT_ERROR, this.onConnectError);
74-
mSocket.off("new message", onNewMessage);
75-
mSocket.off("user joined", onUserJoined);
76-
mSocket.off("user left", onUserLeft);
77-
mSocket.off("typing", onTyping);
78-
mSocket.off("stop typing", onStopTyping);
69+
mSocket.off("new message", this.onNewMessage);
70+
mSocket.off("private message", this.onPrivateMessage);
71+
mSocket.off("user joined", this.onUserJoined);
72+
mSocket.off("user left", this.onUserLeft);
73+
mSocket.off("typing", this.onTyping);
74+
mSocket.off("stop typing", this.onStopTyping);
7975

8076
if (requireActivity() instanceof MainActivity activity) {
8177
leaveRoom(activity);
8278
}
8379
super.onDestroy();
8480
}
81+
82+
@Override
83+
public void onAttach(@NonNull Context context) {
84+
super.onAttach(context);
85+
this.mAdapter = new ChatAdapter(context, this.mItems);
86+
}
8587

8688
@Override
8789
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
@@ -231,6 +233,10 @@ private void scrollToBottom() {
231233
addMessage(username, message);
232234
});
233235

236+
private final Emitter.Listener onPrivateMessage = args -> requireActivity().runOnUiThread(() -> {
237+
238+
});
239+
234240
private final Emitter.Listener onUserJoined = args -> requireActivity().runOnUiThread(() -> {
235241
JSONObject data = (JSONObject) args[0];
236242
String username;

mobile/src/main/java/io/syslogic/socketio/recyclerview/ChatAdapter.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.syslogic.socketio.recyclerview;
22

3+
import android.content.Context;
34
import android.view.LayoutInflater;
45
import android.view.ViewGroup;
56

@@ -17,7 +18,7 @@
1718
public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {
1819
private final ArrayList<ChatMessage> mItems;
1920

20-
public ChatAdapter(@NonNull ArrayList<ChatMessage> items) {
21+
public ChatAdapter(Context context, @NonNull ArrayList<ChatMessage> items) {
2122
mItems = items;
2223
}
2324

server/index.js

+18-19
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
1-
var path = require('path');
2-
var express = require('express');
3-
var port = process.env.PORT || 3000;
1+
const path = require('path');
2+
const express = require('express');
3+
const port = process.env.PORT || 3000;
44

5-
var app = express();
5+
const app = express();
66
app.use(express.static(path.join(__dirname, 'public')));
7-
var server = require('http').createServer(app);
8-
9-
// Close the server if listening doesn't fail
10-
// server.once('listening', function() {
11-
// console.log('Closing port %d...', port);
12-
// server.close();
13-
// });
7+
const server = require('http').createServer(app);
8+
const io = require('socket.io') (server, {
9+
transports: ['polling', 'websocket'],
10+
path: '/socket.io'
11+
});
1412

1513
server.listen(port, () => {
1614
console.log('Listening at port %d', port);
1715
});
1816

19-
var io = require('socket.io')(server, {
20-
transports: ['polling', 'websocket'],
21-
path: '/socket.io'
22-
});
23-
2417
// Chatroom
25-
var numUsers = 0;
18+
let numUsers = 0;
2619

20+
// On connection ...
2721
io.on('connection', (socket) => {
2822

29-
var addedUser = false;
30-
23+
console.log('new client has connected from %s', socket.address);
3124
socket.join('default');
3225

26+
let addedUser = false;
27+
3328
// when the client emits 'new message', this listens and executes
3429
socket.on('new message', (data) => {
3530
console.log('user %s wrote: %s', socket.username, data);
@@ -39,6 +34,10 @@ io.on('connection', (socket) => {
3934
});
4035
});
4136

37+
socket.on("private message", (anotherSocketId, message) => {
38+
socket.to(anotherSocketId).emit("private message", socket.id, message);
39+
});
40+
4241
// when the client emits 'add user', this listens and executes
4342
socket.on('add user', (username) => {
4443

0 commit comments

Comments
 (0)