Skip to content

Instantly share code, notes, and snippets.

@joyceHong0524
Last active September 9, 2019 10:26
Show Gist options
  • Save joyceHong0524/ab20fc083bf06981489a66e76a4dc070 to your computer and use it in GitHub Desktop.
Save joyceHong0524/ab20fc083bf06981489a66e76a4dc070 to your computer and use it in GitHub Desktop.
node.js app
/**
* @author Joyce Hong
* @email [email protected]
* @create date 2019-09-02 20:51:10
* @modify date 2019-09-02 20:51:10
* @desc socket.io server !
*/
const express = require('express');
const bodyParser = require('body-parser');
const socketio = require('socket.io')
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var server = app.listen(3000,()=>{
console.log('Server is running on port number 3000')
})
//Chat Server
var io = socketio.listen(server)
io.on('connection',function(socket) {
//The moment one of your client connected to socket.io server it will obtain socket id
//Let's print this out.
console.log(`Connection : SocketId = ${socket.id}`)
//Since we are going to use userName through whole socket connection, Let's make it global.
var userName = '';
socket.on('subscribe', function(data) {
console.log('subscribe trigged')
const room_data = JSON.parse(data)
userName = room_data.userName;
const roomName = room_data.roomName;
socket.join(`${roomName}`)
console.log(`Username : ${userName} joined Room Name : ${roomName}`)
// Let the other user get notification that user got into the room;
// It can be use to indicate that person has read the messages. (Like turns "unread" into "read")
//TODO: need to chose
//io.to : User who has joined can get a event;
//socket.broadcast.to : all the users except the user who has joined will get the message
// socket.broadcast.to(`${roomName}`).emit('newUserToChatRoom',userName);
io.to(`${roomName}`).emit('newUserToChatRoom',userName);
})
socket.on('unsubscribe',function(data) {
console.log('unsubscribe trigged')
const room_data = JSON.parse(data)
const userName = room_data.userName;
const roomName = room_data.roomName;
console.log(`Username : ${userName} leaved Room Name : ${roomName}`)
socket.broadcast.to(`${roomName}`).emit('userLeftChatRoom',userName)
socket.leave(`${roomName}`)
})
socket.on('newMessage',function(data) {
console.log('newMessage triggered')
const messageData = JSON.parse(data)
const messageContent = messageData.messageContent
const roomName = messageData.roomName
console.log(`[Room Number ${roomName}] ${userName} : ${messageContent}`)
// Just pass the data that has been passed from the writer socket
const chatData = {
userName : userName,
messageContent : messageContent,
roomName : roomName
}
socket.broadcast.to(`${roomName}`).emit('updateChat',JSON.stringify(chatData)) // Need to be parsed into Kotlin object in Kotlin
})
//If you want to add typing function you can make it like this.
// socket.on('typing',function(roomNumber){ //Only roomNumber is needed here
// console.log('typing triggered')
// socket.broadcast.to(`${roomNumber}`).emit('typing')
// })
// socket.on('stopTyping',function(roomNumber){ //Only roomNumber is needed here
// console.log('stopTyping triggered')
// socket.broadcast.to(`${roomNumber}`).emit('stopTyping')
// })
socket.on('disconnect', function () {
console.log("One of sockets disconnected from our server.")
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment