Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. const express = require('express');
  2. require('dotenv').config();
  3. const cors = require('cors');
  4. const path = require('path');
  5. const app = express();
  6. const socket = require('socket.io');
  7. const port = 4203;
  8. const server = app.listen(port, () => {
  9. console.log(`Waiting for connections on ::${port}`);
  10. });
  11.  
  12. app.use(cors());
  13.  
  14. // Static Files
  15. if(process.env.NODE_ENV==="production") {
  16. app.use(express.static(path.join(__dirname,'client/build')));
  17. app.get("*", (req,res) => {
  18. res.sendFile(path.join(__dirname,"client/build","index.html"));
  19. });
  20. }
  21.  
  22. // Ready the input/output
  23. const io = socket(server);
  24.  
  25. io.on('connection', (socket) => {
  26. console.log(`connection established via id: ${socket.id}`);
  27. socket.on('online', () => {
  28. console.log('A new user has joined the chat');
  29. io.sockets.emit('joined',{
  30. 'success':true,
  31. });
  32. });
  33.  
  34. socket.on('chat', (data) => {
  35. // console.log('initiated',data);
  36. io.sockets.emit('chat', data);
  37. });
  38.  
  39. socket.on('typing', (data) => {
  40. socket.broadcast.emit('typing',data);
  41. });
  42. socket.on('no_typing', (data) => {
  43. socket.broadcast.emit('no_typing',data);
  44. });
  45. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement