Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const app = require('http').createServer(handler)
  2. const io = require('socket.io')(app)
  3. const fs = require('fs')
  4.  
  5. let clients = []
  6.  
  7. app.listen(8080)
  8.  
  9. function handler(req, res) {
  10.  
  11.     if (req.url === '/') {
  12.         fs.readFile(__dirname + '/index.html', (err, data) => {
  13.             if (err) {
  14.                 res.writeHead(500)
  15.                 return res.end('Error loading index.html')
  16.             } else {
  17.                 res.writeHead(200)
  18.                 res.end(data)
  19.             }
  20.         })
  21.     } else if (req.headers.referer) {
  22.  
  23.         fs.readFile(__dirname + req.url, (err, data) => {
  24.             if (err) console.log('Error loading ' + req.url)
  25.             else {
  26.                 res.writeHead(200)
  27.                 res.end(data)
  28.             }
  29.         })
  30.  
  31.     }
  32.  
  33. }
  34.  
  35. io.on('connection', (socket) => {
  36.  
  37.     console.log('connection')
  38.    
  39.     let client = {}
  40.     client.socket = socket
  41.    
  42.     socket.on('userlist', (data) => {
  43.        
  44.         let canConnect = true
  45.        
  46.         if(clients.map(x => x.username).indexOf(data) >= 0){
  47.             canConnect = false
  48.         }
  49.        
  50.         socket.emit('canConnect', canConnect)
  51.        
  52.     })
  53.  
  54.     socket.on('username', (data) => {
  55.         client.username = data
  56.         clients.push(client)
  57.  
  58.         clients.forEach((e) => {
  59.             e.socket.emit('userslist', clients.map(x => x.username))
  60.         })
  61.  
  62.     })
  63.    
  64.     socket.on('message', (data) => {
  65.         sendMessage(data)
  66.     })
  67.  
  68.     socket.on('disconnect', () => {
  69.  
  70.         console.log('user disconnected')
  71.  
  72.         const i = clients.map(x => x.socket).indexOf(socket)
  73.         clients.splice(i, 1)
  74.  
  75.         clients.forEach((e) => {
  76.             e.socket.emit('userslist', clients.map(x => x.username))
  77.         })
  78.  
  79.     })
  80.  
  81. });
  82.  
  83. function sendMessage(data) {
  84.    
  85.     if(data.to){
  86.         sendPrivateMessage(data)
  87.         return
  88.     }
  89.  
  90.     clients.forEach((e) => {
  91.  
  92.         e.socket.emit('message', getDate() + ' ' + data.username + ' : ' + data.message)
  93.  
  94.     })
  95.  
  96. }
  97.  
  98. function sendPrivateMessage(data){
  99.    
  100.     const sendTo = clients.map( x => x.username).indexOf(data.to)
  101.     if(sendTo < 0){}
  102.         //tutaj error nie znaleziono usera  
  103.     else{
  104.         const sendToSocket = clients[sendTo].socket
  105.         const obj = {}
  106.         obj.message = getDate() + ' ' + data.username + ' : ' + data.message
  107.         obj.from = data.username
  108.         sendToSocket.emit('privatemessage', obj)
  109.        
  110.         obj.from = data.to
  111.         obj.echo = true
  112.         clients[clients.map(x => x.username).indexOf(data.username)].socket.emit('privatemessage', obj)
  113.        
  114.     }
  115.        
  116. }
  117.  
  118. function getDate() {
  119.     const date = new Date()
  120.     const hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  121.     const minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  122.     const seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  123.     return hours + ":" + minutes + ":" + seconds
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement