Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*var io = require('C:/Program Files (x86)/nodejs/node_modules/socket.io/index.js').listen(8080);
- io.sockets.on('connection', function (socket) {
- console.log("server connection is stated");
- socket.emit('news', { hello: 'world' });
- socket.on('my other event', function (data) {
- console.log(data);
- });
- });
- var http = require('http');
- http.createServer(function (request, response) {
- response.writeHead(200, {'Content-Type': 'text/plain'});
- response.end('Hello World\n');
- }).listen(80);
- */
- //===================================== second implimentation
- /*
- var app = require('http').createServer(handler)
- , io = require('C:/Program Files (x86)/nodejs/node_modules/socket.io/index.js').listen(app)
- , fs = require('fs');
- io.set('log level', 1);
- app.listen(80);
- function handler (req, res) {
- fs.readFile(__dirname + '/index.html',
- function (err, data) {
- if (err) {
- res.writeHead(500);
- return res.end('Error loading index.html');
- }
- res.writeHead(200,{'Content-Type':'text/html'});
- //res.writeHead(200);
- res.end(data);
- });
- }
- var clients = {};
- var user_count = 0;
- io.sockets.on('connection', function (socket) {
- var address = socket.handshake.address.address + ":" + socket.handshake.address.port;
- var unique_id = socket.id;
- if(address in clients){
- clients[address].push(unique_id);
- }else{
- clients[address] = [unique_id];
- }
- console.log(socket);
- user_count+=1;
- socket.namespace.name = "user#" + user_count;
- socket.emit('client_listing', {data:JSON.stringify(clients)});
- socket.on('disconnect',function(){
- console.log("============= disconnect ============");
- if(count(clients[address].ids) > 1){
- clients[address].ids[unique_id].delete;
- }else{
- clients[address].delete;
- }
- console.log(clients);
- });
- });
- */
- //======================================= third implimentation
- /*
- var app = require('http').createServer(handler)
- , io = require('C:/Program Files (x86)/nodejs/node_modules/socket.io/index.js').listen(app)
- , fs = require('fs');
- io.set('log level', 1);
- app.listen(80);
- function handler (req, res) {
- fs.readFile(__dirname + '/index.html',
- function (err, data) {
- if (err) {
- res.writeHead(500);
- return res.end('Error loading index.html');
- }
- res.writeHead(200,{'Content-Type':'text/html'});
- res.end(data);
- });
- }
- var clients = {};
- var game_room = {};
- var game_count = 0;
- var lobby = io
- .of('/lobby')
- .on('connection',function(socket){
- var address = socket.handshake.address.address + ":" + socket.handshake.address.port;
- var unique_id = socket.id;
- console.log("Lobby - " + address + " id:" + unique_id);
- if(address in clients){
- clients[address].push(unique_id);
- }else{
- clients[address]= [unique_id];
- }
- socket.on('register',function(data,fn){
- console.log(data);
- fn(unique_id);
- if(game_count>0){
- io.of('/lobby').emit('game room list',JSON.stringify({
- type:'list',
- data:game_room
- }));
- }
- console.log(data);
- });
- socket.on('create game room',function(data,fn){
- console.log('someone is requesting for a game, id:' + data);
- if(data in game_room){
- fn(false);
- }else{
- game_room[data] = {
- host:data,
- opponent:''
- };
- game_count++;
- fn(true);
- io.of('/lobby').emit('game room list',JSON.stringify({
- type:'single',
- data:game_room[data]
- }));
- }
- });
- socket.on('join game room',function(data,fn){
- if(data in game_room){
- if(game_room[data].opponent == ""){
- game_room[data].opponent = this.unique_id;
- fn(JSON.stringify({
- type:'success',
- msg:'Successfully joined a game',
- game_data:game_room[data]
- }));
- }else{
- fn(JSON.stringify({
- type:'error',
- msg:'This Game Room has already an opponent'
- }));
- }
- }else{
- fn(JSON.stringify({
- type:'error',
- msg:'Unable to find Game Room'
- }));
- }
- });
- socket.on('cancel game room',function(data,fn){
- });
- });
- var game = io
- .of('/game')
- .on('connection',function(socket){
- var address = socket.handshake.address.address + ":" + socket.handshake.address.port;
- var unique_id = socket.id;
- console.log("Game - " + address + " id:" + unique_id);
- });
- */
- //=========================== fourth implimentation
- var app = require('http').createServer(handler)
- , io = require('C:/Program Files (x86)/nodejs/node_modules/socket.io/index.js').listen(app)
- , fs = require('fs');
- io.set('log level', 1);
- app.listen(80);
- function handler (req, res) {
- fs.readFile(__dirname + '/index.html',
- function (err, data) {
- if (err) {
- res.writeHead(500);
- return res.end('Error loading index.html');
- }
- res.writeHead(200,{'Content-Type':'text/html'});
- res.end(data);
- });
- }
- var clients = {};
- var game_room = {};
- var game_count = 0;
- var lobby = io
- .of('/lobby')
- .on('connection',function(socket){
- var address = socket.handshake.address.address + ":" + socket.handshake.address.port;
- var unique_id = socket.id;
- console.log("Lobby - " + address + " id:" + unique_id);
- socket.on('REGISTER',function(data,fn){
- fn(unique_id);
- });
- socket.on('SEND MESSAGE TO ALL',function(data,fn){
- io.of('/lobby').emit('LOBBY MESSAGE',JSON.stringify({
- from:unique_id,
- msg:data
- }));
- fn(true);
- });
- socket.on('CREATE ROOM',function(){
- socket.join(unique_id);
- if(unique_id in game_room){
- game_room[unique_id].clients.push(unique_id);
- }else{
- game_room[unique_id] = {
- clients:[unique_id]
- };
- }
- io.of('/lobby').in(unique_id).emit('ROOM CHAT',"New client connected to this room. Client ID: " + unique_id);
- update_room_list();
- });
- socket.on('JOIN ROOM',function(data,fn){
- socket.join(data);
- fn(true);
- console.log(data);
- game_room[data].clients.push(unique_id);
- io.of('/lobby').in(data).emit('ROOM CHAT',"New client connected to this room. Client ID: " + unique_id);
- update_room_list();
- });
- update_room_list = function(){
- io.of('/lobby').emit('GAME LIST',JSON.stringify(game_room));
- };
- });
Advertisement
Add Comment
Please, Sign In to add comment