Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var
- http = require('http'),
- express = require('express'),
- mysql = require('promise-mysql'),
- mysql2 = require('mysql'),
- parser = require('body-parser'),
- path = require("path"),
- fs = require('fs'),
- app = express(),
- serverhttp = http.createServer(app),
- io = require('socket.io')(serverhttp),
- pool = mysql.createPool({
- host: 'localhost',
- user: 'root',
- password: '',
- database: 'chat_corporativo',
- connectionLimit: 100
- });
- io.on('connection', function(socket){
- clients.push(socket);
- console.log('Conexão: %s sockets conectados! ID: %s', clients.length, socket.id);
- socket.on('ao_conectar_usuario', (data) => {
- pool.query('select * from usuarios where USUCOD = ?', [data]).
- then((rows) => {
- if (rows.length != 0){
- let Usuariologado = rows[0].USUON;
- if (Usuariologado == 'N') {
- let usuario =
- {
- conexaonova : {
- idsocket: socket.id,
- codigousuario: data
- }
- };
- pool.query("update usuarios set USUON = 'S', USUSOCKET = ? where USUCOD = ?", [socket.id, data]).
- then((rows) => {
- io.emit(usuario);
- }).catch((err) => {
- console.log(err);
- });
- } else {
- let negarconexao =
- {
- conexaonova : {
- idsocket: '',
- porque: 'logado'}
- };
- socket.emit(negarconexao);
- };
- }
- }).catch((err) => {
- console.log(err);
- });
- });
- socket.on('mensagem_privada', (id, msg) => {
- console.log(id + ' - ' + msg);
- var usuario_enviou;
- var usuario_receber;
- pool.query("select * from usuarios where USUSOCKET = '?'", [id])
- .then((rows) =>{
- usuario_enviou = rows[0].CONCOD;
- }).catch((err) => {
- console.log(err);
- });
- pool.query('select * from usuarios where USUSOCKET = ?', [socket.id])
- .then((rows) =>{
- usuario_receber = rows[0].CONCOD;
- }).catch((err) => {
- console.log(err);
- });
- });
Add Comment
Please, Sign In to add comment