paulon

Untitled

Mar 8th, 2012
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. /*var io = require('C:/Program Files (x86)/nodejs/node_modules/socket.io/index.js').listen(8080);
  2.  
  3. io.sockets.on('connection', function (socket) {
  4. console.log("server connection is stated");
  5. socket.emit('news', { hello: 'world' });
  6. socket.on('my other event', function (data) {
  7. console.log(data);
  8. });
  9. });
  10.  
  11. var http = require('http');
  12.  
  13. http.createServer(function (request, response) {
  14. response.writeHead(200, {'Content-Type': 'text/plain'});
  15. response.end('Hello World\n');
  16. }).listen(80);
  17. */
  18. //===================================== second implimentation
  19. /*
  20. var app = require('http').createServer(handler)
  21. , io = require('C:/Program Files (x86)/nodejs/node_modules/socket.io/index.js').listen(app)
  22. , fs = require('fs');
  23.  
  24.  
  25. io.set('log level', 1);
  26.  
  27. app.listen(80);
  28. function handler (req, res) {
  29. fs.readFile(__dirname + '/index.html',
  30. function (err, data) {
  31. if (err) {
  32. res.writeHead(500);
  33. return res.end('Error loading index.html');
  34. }
  35.  
  36. res.writeHead(200,{'Content-Type':'text/html'});
  37. //res.writeHead(200);
  38. res.end(data);
  39. });
  40. }
  41. var clients = {};
  42. var user_count = 0;
  43. io.sockets.on('connection', function (socket) {
  44. var address = socket.handshake.address.address + ":" + socket.handshake.address.port;
  45. var unique_id = socket.id;
  46.  
  47. if(address in clients){
  48. clients[address].push(unique_id);
  49. }else{
  50. clients[address] = [unique_id];
  51.  
  52. }
  53. console.log(socket);
  54. user_count+=1;
  55. socket.namespace.name = "user#" + user_count;
  56. socket.emit('client_listing', {data:JSON.stringify(clients)});
  57.  
  58.  
  59. socket.on('disconnect',function(){
  60. console.log("============= disconnect ============");
  61. if(count(clients[address].ids) > 1){
  62. clients[address].ids[unique_id].delete;
  63. }else{
  64. clients[address].delete;
  65. }
  66. console.log(clients);
  67. });
  68.  
  69. });
  70. */
  71. //======================================= third implimentation
  72.  
  73. /*
  74. var app = require('http').createServer(handler)
  75. , io = require('C:/Program Files (x86)/nodejs/node_modules/socket.io/index.js').listen(app)
  76. , fs = require('fs');
  77.  
  78.  
  79. io.set('log level', 1);
  80.  
  81. app.listen(80);
  82. function handler (req, res) {
  83. fs.readFile(__dirname + '/index.html',
  84. function (err, data) {
  85. if (err) {
  86. res.writeHead(500);
  87. return res.end('Error loading index.html');
  88. }
  89. res.writeHead(200,{'Content-Type':'text/html'});
  90. res.end(data);
  91. });
  92. }
  93. var clients = {};
  94. var game_room = {};
  95. var game_count = 0;
  96. var lobby = io
  97. .of('/lobby')
  98. .on('connection',function(socket){
  99. var address = socket.handshake.address.address + ":" + socket.handshake.address.port;
  100. var unique_id = socket.id;
  101. console.log("Lobby - " + address + " id:" + unique_id);
  102. if(address in clients){
  103. clients[address].push(unique_id);
  104. }else{
  105. clients[address]= [unique_id];
  106. }
  107.  
  108. socket.on('register',function(data,fn){
  109. console.log(data);
  110. fn(unique_id);
  111. if(game_count>0){
  112. io.of('/lobby').emit('game room list',JSON.stringify({
  113. type:'list',
  114. data:game_room
  115. }));
  116. }
  117. console.log(data);
  118. });
  119. socket.on('create game room',function(data,fn){
  120. console.log('someone is requesting for a game, id:' + data);
  121. if(data in game_room){
  122. fn(false);
  123. }else{
  124. game_room[data] = {
  125. host:data,
  126. opponent:''
  127. };
  128. game_count++;
  129. fn(true);
  130. io.of('/lobby').emit('game room list',JSON.stringify({
  131. type:'single',
  132. data:game_room[data]
  133. }));
  134. }
  135. });
  136. socket.on('join game room',function(data,fn){
  137. if(data in game_room){
  138. if(game_room[data].opponent == ""){
  139. game_room[data].opponent = this.unique_id;
  140. fn(JSON.stringify({
  141. type:'success',
  142. msg:'Successfully joined a game',
  143. game_data:game_room[data]
  144. }));
  145. }else{
  146. fn(JSON.stringify({
  147. type:'error',
  148. msg:'This Game Room has already an opponent'
  149. }));
  150. }
  151. }else{
  152. fn(JSON.stringify({
  153. type:'error',
  154. msg:'Unable to find Game Room'
  155. }));
  156. }
  157. });
  158. socket.on('cancel game room',function(data,fn){
  159.  
  160. });
  161. });
  162. var game = io
  163. .of('/game')
  164. .on('connection',function(socket){
  165. var address = socket.handshake.address.address + ":" + socket.handshake.address.port;
  166. var unique_id = socket.id;
  167. console.log("Game - " + address + " id:" + unique_id);
  168. });
  169. */
  170. //=========================== fourth implimentation
  171. var app = require('http').createServer(handler)
  172. , io = require('C:/Program Files (x86)/nodejs/node_modules/socket.io/index.js').listen(app)
  173. , fs = require('fs');
  174.  
  175.  
  176. io.set('log level', 1);
  177.  
  178. app.listen(80);
  179. function handler (req, res) {
  180. fs.readFile(__dirname + '/index.html',
  181. function (err, data) {
  182. if (err) {
  183. res.writeHead(500);
  184. return res.end('Error loading index.html');
  185. }
  186. res.writeHead(200,{'Content-Type':'text/html'});
  187. res.end(data);
  188. });
  189. }
  190. var clients = {};
  191. var game_room = {};
  192. var game_count = 0;
  193. var lobby = io
  194. .of('/lobby')
  195. .on('connection',function(socket){
  196. var address = socket.handshake.address.address + ":" + socket.handshake.address.port;
  197. var unique_id = socket.id;
  198. console.log("Lobby - " + address + " id:" + unique_id);
  199.  
  200. socket.on('REGISTER',function(data,fn){
  201. fn(unique_id);
  202. });
  203. socket.on('SEND MESSAGE TO ALL',function(data,fn){
  204. io.of('/lobby').emit('LOBBY MESSAGE',JSON.stringify({
  205. from:unique_id,
  206. msg:data
  207. }));
  208. fn(true);
  209. });
  210. socket.on('CREATE ROOM',function(){
  211. socket.join(unique_id);
  212. if(unique_id in game_room){
  213. game_room[unique_id].clients.push(unique_id);
  214. }else{
  215. game_room[unique_id] = {
  216. clients:[unique_id]
  217. };
  218. }
  219. io.of('/lobby').in(unique_id).emit('ROOM CHAT',"New client connected to this room. Client ID: " + unique_id);
  220. update_room_list();
  221. });
  222. socket.on('JOIN ROOM',function(data,fn){
  223. socket.join(data);
  224. fn(true);
  225. console.log(data);
  226. game_room[data].clients.push(unique_id);
  227. io.of('/lobby').in(data).emit('ROOM CHAT',"New client connected to this room. Client ID: " + unique_id);
  228. update_room_list();
  229. });
  230. update_room_list = function(){
  231. io.of('/lobby').emit('GAME LIST',JSON.stringify(game_room));
  232. };
  233. });
Advertisement
Add Comment
Please, Sign In to add comment