김대연

add socket example server

File mode changed
......@@ -2,9 +2,11 @@ const path = require('path')
require('dotenv').config({path: path.join(__dirname, "../.env")})
const ip = process.env.IP || null
const s_port = process.env.SOCKET_PORT || null
const token = process.env.TOKEN || null
module.exports = {
'ip' : ip,
's_port' : s_port,
'token' : token
}
......
......@@ -2,7 +2,7 @@
"name": "khuwitch-server",
"version": "1.0.0",
"description": "twitch translator chatbot & tts server dev",
"main": "app.js",
"main": "socket_server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
......
const config = require(__dirname + '/config/config')
const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const port = process.env.SOCKET_PORT;
server.listen(port, () => { console.log(`Listening on port ${port}`) });
io.on('connection', socket => {
console.log("connected socketID : ", socket.id);
io.to(socket.id).emit('my socket id',{socketId: socket.id});
socket.on('enter chatroom', () => {
console.log("channel에 입장");
socket.broadcast.emit('receive chat', {type: "alert", chat: "누군가가 입장하였습니다.", regDate: Date.now()});
})
socket.on('send voice', data => {
console.log(`${socket.id} : ${data.chat}`);
io.emit('send voice', data);
})
socket.on('leave chatroom', data => {
console.log('leave chatroom ', data);
socket.broadcast.emit('receive chat', {type: "alert", chat: "누군가가 퇴장하였습니다.", regDate: Date.now()});
})
})
\ No newline at end of file
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
<h1>Socket Chat</h1>
\ No newline at end of file