Guest User

LiveConnect UDP

a guest
Jul 15th, 2012
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function()
  2. {
  3. var BUF_SIZE = 10240;
  4.  
  5. window.netwerk.JavaUDP = Class(netwerk.Net, {
  6.     init: function(location, port)
  7.     {
  8.         this.Super.init.call(this, location);
  9.        
  10.         this.socketChannel = java.nio.channels.DatagramChannel.open();
  11.         this.socketChannel.configureBlocking(false);
  12.         this.socketChannel.socket().bind(null); // If the address is null, then the system will pick up an ephemeral port and a valid local address to bind the socket.
  13.        
  14.         // Note: does not work on localhost
  15.         // And requires your reverse dns to match your domain name
  16.         // (or use an IP to visit the web application)
  17.         this.socketChannel.connect(new java.net.InetSocketAddress(location, port));
  18.        
  19.         this.readByteBuffer = java.nio.ByteBuffer.allocate(BUF_SIZE);
  20.         this.readByteBuffer.clear();
  21.         this.readBuffer = '';
  22.        
  23.         this.writeByteBuffer = java.nio.ByteBuffer.allocate(BUF_SIZE);
  24.         this.writeByteBuffer.clear();
  25.        
  26.         setTimeout(this._readTimer.bind(this), 1);
  27.        
  28.         return this;
  29.     },
  30.    
  31.     send: function(json)
  32.     {
  33.         var buf, message, messageLength, a, len;
  34.        
  35.         message = new java.lang.String(JSON.stringify([json]) + "\n").getBytes('UTF-8');
  36.         messageLength = message.length;
  37.        
  38.         for (a = 0; a < messageLength; a += BUF_SIZE)
  39.         {
  40.             this.writeByteBuffer.clear();
  41.             if (a + BUF_SIZE >= messageLength)
  42.             {
  43.                 len = messageLength - a;
  44.             }
  45.             else
  46.             {
  47.                 len = BUF_SIZE;
  48.             }
  49.            
  50.             this.writeByteBuffer.put(message, a, len);
  51.             this.writeByteBuffer.rewind();
  52.             this.writeByteBuffer.limit(len);
  53.             this.socketChannel.write(this.writeByteBuffer);
  54.         }
  55.        
  56.     },
  57.    
  58.     readyToSend: function()
  59.     {
  60.         return !!this.socketChannel.isConnected();
  61.     },
  62.    
  63.     done: function()
  64.     {
  65.         this._done = true;
  66.         this.socketChannel.close();
  67.         this.socketChannel = null;
  68.         this.readByteBuffer = null;
  69.         this.writeByteBuffer = null;
  70.     },
  71.    
  72.     _readTimer: function()
  73.     {
  74.         var read, lines, a, json, b;
  75.         if (this._done) return;
  76.        
  77.         try
  78.         {
  79.             if (!this.socketChannel.isConnected()) return;
  80.            
  81.             while(true)
  82.             {
  83.                 this.readByteBuffer.clear();
  84.                 read = this.socketChannel.read(this.readByteBuffer);
  85.                
  86.                 if (read == 0) return;
  87.                 if (read < 0)
  88.                 {
  89.                     this.done();
  90.                     return;
  91.                 }
  92.                
  93.                 this.readBuffer += new java.lang.String(this.readByteBuffer.array(), 0, read, 'UTF-8') + ''; // impliciet java string naar javascript string
  94.                
  95.                 lines = this.readBuffer.split('\n');
  96.                 try
  97.                 {
  98.                     for (a = 0; a < lines.length - 1; ++a)
  99.                     {
  100.                         if (!lines[a]) continue; // lege regel
  101.                          
  102.                         try
  103.                         {
  104.                             json = JSON.parse(lines[a]);
  105.                         }
  106.                         catch(err)
  107.                         {
  108.                             if (window.console)
  109.                             {
  110.                                 console.log('Unable to parse message from server as json', err);
  111.                             }
  112.                             continue;
  113.                         }
  114.                        
  115.                         for (b = 0; b < json.length; b++)
  116.                         {
  117.                             if (json[b] != null)
  118.                             {
  119.                                 this.onmessage(json[b]);
  120.                             }
  121.                         }
  122.                     }
  123.                 }
  124.                 finally
  125.                 {      
  126.                     this.readBuffer = lines[lines.length-1];
  127.                 }
  128.             }
  129.         }
  130.         finally
  131.         {
  132.             setTimeout(this._readTimer.bind(this), 1);
  133.         }
  134.     }
  135. });
  136.  
  137. window.netwerk.JavaUDP.isSupported = function()
  138. {
  139.     return !!window.java;
  140. };
  141.    
  142. })();
Add Comment
Please, Sign In to add comment