revent 0.2 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/revent/Client.as CHANGED
@@ -2,6 +2,7 @@
2
2
  import flash.events.*;
3
3
  import flash.net.Socket;
4
4
  import flash.net.ObjectEncoding;
5
+ import flash.utils.ByteArray;
5
6
 
6
7
  public class Client extends EventDispatcher {
7
8
  private static const TYPE_CALL:int = 0;
@@ -10,6 +11,23 @@
10
11
 
11
12
  private var _socket:Socket;
12
13
 
14
+ public static function bytesToByteArray(bytes:Array):ByteArray {
15
+ var ba:ByteArray = new ByteArray();
16
+ ba.objectEncoding = ObjectEncoding.AMF3;
17
+ for (var i:int = 0; i < bytes.length; i++) {
18
+ ba.writeByte(bytes[i]);
19
+ }
20
+ return ba;
21
+ }
22
+
23
+ public static function bytesToObject(bytes:Array, compressed:Boolean = false):Object {
24
+ var ba:ByteArray = bytesToByteArray(bytes);
25
+ if (compressed) {
26
+ ba.uncompress();
27
+ }
28
+ return ba.readObject();
29
+ }
30
+
13
31
  public function Client():void {
14
32
  _socket = new Socket();
15
33
  _socket.objectEncoding = ObjectEncoding.AMF3;
@@ -23,6 +23,7 @@ module RubyAMF
23
23
  def reset_referencables
24
24
  @stored_strings = {} # hash is way faster than array
25
25
  @stored_strings[""] = true # add this in automatically
26
+ @floats_cache = {} # for write_double at read_write.rb
26
27
  @write_amf3_integer_results = {} # cache the integers
27
28
  @current_strings_index = 0
28
29
  end
data/lib/revent/as_r.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'eventmachine'
3
+ require 'zlib'
3
4
  require "#{File.dirname(__FILE__)}/amf3/amf3"
4
5
 
5
6
  # AS clients are not reliable. For security, we close the connection immediately
@@ -107,22 +108,47 @@ module Revent
107
108
  def call(cmd, value, close_connection_after_writing = false)
108
109
  send_amf3_data([TYPE_CALL, cmd, value])
109
110
  self.close_connection_after_writing if close_connection_after_writing
111
+ rescue
112
+ @me.logger.error($!)
110
113
  end
111
114
 
112
115
  def result(cmd, value, close_connection_after_writing = false)
113
116
  send_amf3_data([TYPE_RESULT, cmd, value])
114
117
  self.close_connection_after_writing if close_connection_after_writing
115
118
  rescue
119
+ @me.logger.error($!)
116
120
  end
117
121
 
118
122
  def error(cmd, value, close_connection_after_writing = false)
119
123
  send_amf3_data([TYPE_ERROR, cmd, value])
120
124
  self.close_connection_after_writing if close_connection_after_writing
125
+ rescue
126
+ @me.logger.error($!)
127
+ end
128
+
129
+ def compress(value)
130
+ @enc.reset
131
+ @enc.write_amf3(value)
132
+ s = @enc.stream
133
+ @enc.reset
134
+
135
+ s = Zlib::Deflate.deflate(s)
136
+ s.unpack('c*')
121
137
  end
122
138
  end
123
139
 
124
140
  # To make your class a server, just include Revent::RRServer in it.
125
141
  module ASRServer
142
+ # Compresses using zlib and converts the input to an array of bytes.
143
+ def self.compress(value)
144
+ enc = RubyAMF::IO::AMFSerializer.new
145
+ enc.write_amf3(value)
146
+ s = enc.stream
147
+
148
+ s = Zlib::Deflate.deflate(s)
149
+ s.unpack('c*')
150
+ end
151
+
126
152
  # For security check, normally command cannot be too long
127
153
  DEFAULT_MAX_CMD_LENGTH = 1024
128
154
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: revent
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.2"
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ngoc DAO Thanh