Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // A socket.io alapú chat magja.
- var socketio = require('socket.io');
- var mysql = require('mysql');
- var connect = require('connect');
- var cookieParser = require('cookie-parser');
- var pool = mysql.createPool({
- host: 'localhost',
- user: 'root',
- password: 'root',
- database: 'lookingforgroup',
- connectionLimit: 100,
- debug: false,
- multipleStatements: true
- });
- // A függvényt a /bin/www fájlból hívjuk meg. A függvényen belül eseményeket definiálunk.
- function sockets(server) {
- var io = socketio(server);
- // Ha kapcsolódva van egy user a chathez.
- io.on('connection', function(socket){
- // Chatről való lekapcsolódáskor ez történjen
- socket.on('disconnect', function(){
- });
- // Feliratkozás az értesítés-"szobába", illetve a chatszobába.
- socket.on('subscribe', function (room) {
- socket.join(room);
- });
- socket.on('send_joinrequest', function(data) {
- pool.getConnection(function(err, connection) {
- var offerMembershipColumnName = "";
- switch (data.offerType) {
- case "guild":
- offerMembershipColumnName = "member_of_guild";
- break;
- case "arena2v2":
- offerMembershipColumnName = "member_of_2v2";
- break;
- case "arena3v3":
- offerMembershipColumnName = "member_of_3v3";
- break;
- case "arena5v5":
- offerMembershipColumnName = "member_of_5v5";
- break;
- }
- connection.query('SELECT ' + offerMembershipColumnName + ' FROM wow_chars WHERE char_id = ?', [data.charData.charId], function(err, results) {
- if (err) {
- connection.release();
- throw err;
- }
- if (results[0][offerMembershipColumnName] == null) {
- connection.query('INSERT INTO wow_joinrequests(char_id, offer_id, state) VALUES (?,?,?)', [data.charData.charId, data.offerId, 0], function(err, results2) {
- if (err) {
- connection.release();
- throw err;
- }
- connection.query('SELECT user_id, title FROM wow_offers WHERE offer_id = ?', [data.offerId], function(err, results3) {
- if (err) {
- connection.release();
- throw err;
- }
- var userToNotify = "user_" + results3[0].user_id;
- console.log(userToNotify);
- var userToNotifyOnlyId = userToNotify.split("_")[1];
- var notifyMessage = "<a href='/wow/profile/" + data.userData.user_id + "'>" +
- data.userData.battletag + "</a> felhasználó a <a href='/wow/character/" +
- data.charData.charId + "'>" + data.charData.charName + "</a> karakterével fejlenetkezett a <a href='/wow/offers/" +
- data.offerId + "'>" + results3[0].title + "</a> című hirdetésre.";
- socket.join(userToNotify);
- socket.broadcast.to(userToNotify).emit('receive_notify', {
- message: notifyMessage
- });
- socket.leave(userToNotify);
- var selfNotify = "user_" + data.userData.userId;
- var alertMessage = "Sikeres jelentkezés!";
- socket.nsp.to(selfNotify).emit('receive_alert', {
- message: alertMessage
- });
- connection.query('INSERT INTO wow_notifications(user_id, text, seen) VALUES (?,?,?)', [userToNotifyOnlyId, notifyMessage, 0], function(err, results4) {
- if (err) {
- connection.release();
- throw err;
- }
- connection.release();
- });
- });
- });
- } else {
- connection.release();
- var selfNotify = "user_" + data.userData.userId;
- var alertMessage = "Ezzel a karaktereddel már tagja vagy egy " + data.offerType + " típusú csoportnak.";
- socket.nsp.to(selfNotify).emit('receive_alert', {
- message: alertMessage
- });
- }
- });
- });
- });
- socket.on('accept_joinrequest', function(data) {
- pool.getConnection(function(err, connection) {
- connection.query('UPDATE wow_joinrequests SET state = ? WHERE request_id = ? AND state = ?;', [1, data.requestId, 0], function(err, results) {
- if (err) {
- connection.release();
- throw err;
- }
- if (results.affectedRows > 0) {
- var userToNotify = "user_" + data.charUserId;
- var notifyMessage = "<a href='/wow/profile/" + data.offerUserId + "'>" + data.offerUserBattletag +
- "</a> felhasználó elfogadta a(z) <a href='/wow/character/" + data.charId + "'>" + data.charName +
- "</a> karaktereddel tett jelentkezésed a(z) <a href='wow/offers/" + data.offerId + "'>" + data.offerTitle +
- "</a> hirdetésre.";
- socket.join(userToNotify);
- socket.broadcast.to(userToNotify).emit('receive_notify', {
- message: notifyMessage
- });
- socket.leave(userToNotify);
- connection.query('INSERT INTO wow_notifications(user_id, text, seen) VALUES(?,?,?)', [data.charUserId, notifyMessage, 0], function(err, results2) {
- if (err) {
- connection.release();
- throw err;
- }
- connection.release();
- });
- } else {
- connection.release();
- var selfNotify = "user_" + data.offerUserId;
- var alertMessage = "A jelentkezés állapota az utolsó betöltés óta megváltozott. Töltsd újra az oldalt.";
- socket.nsp.to(selfNotify).emit('receive_alert_jreq_fo', {
- message: alertMessage
- });
- }
- });
- });
- });
- socket.on('decline_joinrequest', function(data) {
- pool.getConnection(function(err, connection) {
- connection.query('UPDATE wow_joinrequests SET state = ? WHERE request_id = ? AND (state = ? OR state = ?);', [2, data.requestId, 0, 1], function(err, results) {
- if (err) {
- connection.release();
- throw err;
- }
- if (results.affectedRows > 0) {
- var userToNotify = "user_" + data.charUserId;
- var notifyMessage = "<a href='/wow/profile/" + data.offerUserId + "'>" + data.offerUserBattletag +
- "</a> felhasználó elutasította a(z) <a href='/wow/character/" + data.charId + "'>" + data.charName +
- "</a> karaktereddel tett jelentkezésed a(z) <a href='wow/offers/" + data.offerId + "'>" + data.offerTitle +
- "</a> hirdetésre.";
- socket.join(userToNotify);
- socket.broadcast.to(userToNotify).emit('receive_notify', {
- message: notifyMessage
- });
- socket.leave(userToNotify);
- connection.query('INSERT INTO wow_notifications(user_id, text, seen) VALUES(?,?,?)', [data.charUserId, notifyMessage, 0], function(err, results2) {
- if (err) {
- connection.release();
- throw err;
- }
- connection.release();
- });
- } else {
- connection.release();
- var selfNotify = "user_" + data.offerUserId;
- var alertMessage = "A jelentkezés állapota az utolsó betöltés óta megváltozott. Töltsd újra az oldalt.";
- socket.nsp.to(selfNotify).emit('receive_alert_jreq_fo', {
- message: alertMessage
- });
- }
- });
- });
- });
- socket.on('cancel_joinrequest', function(data) {
- pool.getConnection(function(err, connection) {
- connection.query('UPDATE wow_joinrequests SET state = ? WHERE request_id = ? AND (state = ? OR state = ?);', [3, data.requestId, 0, 1], function(err, results) {
- if (err) {
- connection.release();
- throw err;
- }
- if (results.affectedRows > 0) {
- var userToNotify = "user_" + data.offerUserId;
- console.log(userToNotify);
- var notifyMessage = "<a href='/wow/profile/" + data.charUserId + "'>" + data.charUserBattletag +
- "</a> felhasználó visszavonta a(z) <a href='/wow/character/" + data.charId + "'>" + data.charName +
- "</a> karakterével tett jelentkezését a(z) <a href='wow/offers/" + data.offerId + "'>" + data.offerTitle +
- "</a> hirdetésedre.";
- socket.join(userToNotify);
- socket.broadcast.to(userToNotify).emit('receive_notify', {
- message: notifyMessage
- });
- socket.leave(userToNotify);
- connection.query('INSERT INTO wow_notifications(user_id, text, seen) VALUES(?,?,?)', [data.offerUserId, notifyMessage, 0], function(err, results2) {
- if (err) {
- connection.release();
- throw err;
- }
- connection.release();
- });
- } else {
- connection.release();
- var selfNotify = "user_" + data.charUserId;
- var alertMessage = "A jelentkezés állapota az utolsó betöltés óta megváltozott. Töltsd újra az oldalt.";
- socket.nsp.to(selfNotify).emit('receive_alert_jreq_fm', {
- message: alertMessage
- });
- }
- });
- });
- });
- socket.on('resend_joinrequest', function(data) {
- pool.getConnection(function(err, connection) {
- connection.query('UPDATE wow_joinrequests SET state = ? WHERE request_id = ?;', [0, data.requestId], function(err, results) {
- if (err) {
- connection.release();
- throw err;
- }
- var userToNotify = "user_" + data.offerUserId;
- console.log(userToNotify);
- var notifyMessage = "<a href='/wow/profile/" + data.charUserId + "'>" + data.charUserBattletag +
- "</a> felhasználó újrajelentkezett a(z) <a href='/wow/character/" + data.charId + "'>" + data.charName +
- "</a> karakterével tett jelentkezését a(z) <a href='wow/offers/" + data.offerId + "'>" + data.offerTitle +
- "</a> hirdetésedre.";
- socket.join(userToNotify);
- socket.broadcast.to(userToNotify).emit('receive_notify', {
- message: notifyMessage
- });
- socket.leave(userToNotify);
- connection.query('INSERT INTO wow_notifications(user_id, text, seen) VALUES(?,?,?)', [data.offerUserId, notifyMessage, 0], function(err, results2) {
- if (err) {
- connection.release();
- throw err;
- }
- connection.release();
- });
- });
- });
- });
- socket.on('join_offer', function(data) {
- pool.getConnection(function(err, connection) {
- connection.query('UPDATE wow_joinrequests SET state = ? WHERE request_id = ?;', [4, data.requestId], function(err, results) {
- if (err) {
- connection.release();
- throw err;
- }
- var userToNotify = "user_" + data.offerUserId;
- var notifyMessage = "<a href='/wow/profile/" + data.charUserId + "'>" + data.charUserBattletag +
- "</a> felhasználó belépett a(z) <a href='/wow/character/" + data.charId + "'>" + data.charName +
- "</a> karakterével a(z) <a href='wow/offers/" + data.offerId + "'>" + data.offerTitle +
- "</a> csoportodba.";
- socket.join(userToNotify);
- socket.broadcast.to(userToNotify).emit('receive_notify', {
- message: notifyMessage
- });
- socket.leave(userToNotify);
- connection.query('INSERT INTO wow_notifications(user_id, text, seen) VALUES(?,?,?)', [data.offerUserId, notifyMessage, 0], function(err, results2) {
- if (err) {
- connection.release();
- throw err;
- }
- var offerMembershipColumnName = "";
- switch (data.offerType) {
- case "guild":
- offerMembershipColumnName = "member_of_guild";
- break;
- case "arena2v2":
- offerMembershipColumnName = "member_of_2v2";
- break;
- case "arena3v3":
- offerMembershipColumnName = "member_of_3v3";
- break;
- case "arena5v5":
- offerMembershipColumnName = "member_of_5v5";
- break;
- }
- connection.query('UPDATE wow_chars SET ' + offerMembershipColumnName + ' = ? WHERE char_id = ?', [data.offerId, data.charId], function(err, results3) {
- if (err) {
- connection.release();
- throw err;
- }
- connection.query('UPDATE wow_joinrequests SET state = ? WHERE char_id = ? AND state <> ?;', [5, data.charId, 4], function(err, results4) {
- if (err) {
- connection.release();
- throw err;
- }
- connection.query('INSERT INTO wow_chatroom_memberships(chatroom_id, user_id) VALUES(?,?)', [data.offerChatroomId, data.charUserId], function(err, results5) {
- if (err) {
- connection.release();
- throw err;
- }
- connection.query('INSERT INTO wow_roles(char_id, offer_id, role) VALUES (?,?,?)', [data.charUserId, data.offerId, "Default"], function(err, results6) {
- if (err) {
- connection.release();
- throw err;
- }
- connection.release();
- });
- });
- });
- });
- });
- });
- });
- });
- socket.on('leave_group', function(data) {
- var offerMembershipColumnName = "";
- switch (data.offerType) {
- case "guild":
- offerMembershipColumnName = "member_of_guild";
- break;
- case "arena2v2":
- offerMembershipColumnName = "member_of_2v2";
- break;
- case "arena3v3":
- offerMembershipColumnName = "member_of_3v3";
- break;
- case "arena5v5":
- offerMembershipColumnName = "member_of_5v5";
- break;
- }
- pool.getConnection(function(err, connection) {
- connection.query('UPDATE wow_chars SET ' + offerMembershipColumnName + ' = null WHERE char_id = ?;', [data.charId], function(err, results) {
- if (err) {
- connection.release();
- throw err;
- }
- var userToNotify = "user_" + data.offerUserId;
- var notifyMessage = "<a href='/wow/profile/" + data.charUserId + "'>" + data.charUserBattletag +
- "</a> felhasználó kilépett a(z) <a href='/wow/character/" + data.charId + "'>" + data.charName +
- "</a> karakterével a(z) <a href='wow/offers/" + data.offerId + "'>" + data.offerTitle +
- "</a> csoportból.";
- socket.join(userToNotify);
- socket.broadcast.to(userToNotify).emit('receive_notify', {
- message: notifyMessage
- });
- socket.leave(userToNotify);
- connection.query('UPDATE wow_joinrequests SET state = ? WHERE char_id = ? AND state = ?;', [3, data.charId, 5], function(err, results) {
- if (err) {
- connection.release();
- throw err;
- }
- connection.release();
- });
- });
- });
- });
- socket.on('notifies_seen', function(user_id) {
- pool.getConnection(function(err, connection) {
- connection.query('UPDATE wow_notifications SET seen = ? WHERE user_id = ?;', [1, user_id], function(err, results5) {
- if (err) {
- connection.release();
- throw err;
- }
- connection.release();
- });
- });
- });
- // Üzenetküldés esetén ez történik: az adott chatszobába küldjük az
- // üzenetet úgy, hogy kiváltunk egy eseményt, amely hatására a kliens oldalon
- // megjelenik az üzenet.
- socket.on('send message', function(data) {
- socket.broadcast.to(data.room).emit('conversation private post', {
- // username == lejjebb a subjectName
- username: data.battletag,
- message: data.message
- });
- var convSubjectName = "";
- var convSubjectId = "";
- if (data.chatroom_data.type == 0) {
- convSubjectId = data.user_id;
- convSubjectName = data.battletag;
- } else {
- convSubjectId = data.chatroom_data.offer_id;
- convSubjectName = data.chatroom_data.offer_title;
- }
- // Ha privát chatünk van (négyszemközt), akkor a senderUser == subjectId és senderUserBattletag = ...
- for (var i = 0; i < data.users_to_notify.length; i++) {
- socket.join("user_" + data.users_to_notify[i].user_id);
- socket.broadcast.to("user_" + data.users_to_notify[i].user_id).emit('receive_messagenotify', {
- chatroomId: data.chatroom_data.chatroom_id,
- chatroomType: data.chatroom_data.type,
- subjectId: convSubjectId,
- subjectName: convSubjectName,
- senderUserId: data.user_id,
- senderUserBattletag: data.battletag,
- message: data.message
- });
- socket.leave("user_" + data.users_to_notify[i].user_id);
- }
- // A notifikációt kiküldjük magunknak is. Ez arra lesz jó, hogy ha írunk egy üzenetet,
- // akkor a főoldalon is frissüljön a konverzációlista a - mi általunk küldött - legújabb üzenettel.
- if (data.chatroom_data.type == 0) {
- socket.broadcast.to("user_" + data.user_id).emit('receive_messagenotify', {
- chatroomId: data.chatroom_data.chatroom_id,
- chatroomType: data.chatroom_data.type,
- subjectId: data.users_to_notify[0].user_id,
- subjectName: data.users_to_notify[0].battletag,
- senderUserId: data.user_id,
- senderUserBattletag: data.battletag,
- message: data.message
- });
- } else {
- socket.broadcast.to("user_" + data.user_id).emit('receive_messagenotify', {
- chatroomId: data.chatroom_data.chatroom_id,
- chatroomType: data.chatroom_data.type,
- subjectId: convSubjectId,
- subjectName: convSubjectName,
- senderUserId: data.user_id,
- senderUserBattletag: data.battletag,
- message: data.message
- });
- }
- var messageSeen = "";
- if (data.chatroom_data.type == 0) {
- messageSeen = 0;
- } else {
- messageSeen = 9;
- }
- // Végül az üzenetet beillesztjük az adatbázisba, hogy később is meg lehessen tekinteni.
- pool.getConnection(function(err, connection) {
- connection.query('INSERT INTO wow_chat_messages(chatroom_id, sender_user, text, seen) VALUES (?,?,?,?)', [data.room, data.user_id, data.message, messageSeen], function(err, results) {
- if (err) {
- connection.release();
- throw err;
- }
- connection.release();
- });
- });
- });
- socket.on('message_seen', function(data) {
- pool.getConnection(function(err, connection) {
- connection.query('UPDATE wow_chat_messages SET seen = ? WHERE (chatroom_id = ? AND sender_user = ?);', [1, data.room, data.the_other_user], function(err, results6) {
- if (err) {
- connection.release();
- throw err;
- }
- connection.release();
- socket.broadcast.to(data.room).emit('message_seen_2', data.the_other_user);
- });
- });
- });
- socket.on('writing', function(data) {
- socket.broadcast.to(data.room).emit('writing_2', {
- yesOrNo: data.yesOrNo,
- the_other_user: data.the_other_user
- });
- });
- socket.on('refresh_chat_window', function(room) {
- socket.broadcast.to(room).emit('refresh_chat_window_2', 'something');
- });
- });
- }
- module.exports = sockets;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement