김대연

add socket example server

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