Guest User

Untitled

a guest
Feb 4th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. var
  2. http = require('http'),
  3. express = require('express'),
  4. mysql = require('promise-mysql'),
  5. mysql2 = require('mysql'),
  6. parser = require('body-parser'),
  7. path = require("path"),
  8. fs = require('fs'),
  9. app = express(),
  10. serverhttp = http.createServer(app),
  11. io = require('socket.io')(serverhttp),
  12. pool = mysql.createPool({
  13. host: 'localhost',
  14. user: 'root',
  15. password: '',
  16. database: 'chat_corporativo',
  17. connectionLimit: 100
  18. });
  19.  
  20. io.on('connection', function(socket){
  21. clients.push(socket);
  22. console.log('Conexão: %s sockets conectados! ID: %s', clients.length, socket.id);
  23.  
  24. socket.on('ao_conectar_usuario', (data) => {
  25. pool.query('select * from usuarios where USUCOD = ?', [data]).
  26. then((rows) => {
  27. if (rows.length != 0){
  28. let Usuariologado = rows[0].USUON;
  29. if (Usuariologado == 'N') {
  30.  
  31. let usuario =
  32. {
  33. conexaonova : {
  34. idsocket: socket.id,
  35. codigousuario: data
  36. }
  37. };
  38.  
  39. pool.query("update usuarios set USUON = 'S', USUSOCKET = ? where USUCOD = ?", [socket.id, data]).
  40. then((rows) => {
  41. io.emit(usuario);
  42. }).catch((err) => {
  43. console.log(err);
  44. });
  45. } else {
  46.  
  47. let negarconexao =
  48. {
  49. conexaonova : {
  50. idsocket: '',
  51. porque: 'logado'}
  52. };
  53.  
  54. socket.emit(negarconexao);
  55. };
  56. }
  57. }).catch((err) => {
  58. console.log(err);
  59. });
  60. });
  61.  
  62. socket.on('mensagem_privada', (id, msg) => {
  63. console.log(id + ' - ' + msg);
  64. var usuario_enviou;
  65. var usuario_receber;
  66.  
  67. pool.query("select * from usuarios where USUSOCKET = '?'", [id])
  68. .then((rows) =>{
  69. usuario_enviou = rows[0].CONCOD;
  70. }).catch((err) => {
  71. console.log(err);
  72. });
  73.  
  74. pool.query('select * from usuarios where USUSOCKET = ?', [socket.id])
  75. .then((rows) =>{
  76. usuario_receber = rows[0].CONCOD;
  77. }).catch((err) => {
  78. console.log(err);
  79. });
  80. });
Add Comment
Please, Sign In to add comment