Index: chrome/common/extensions/docs/examples/extensions/native_messaging/echo.rb |
diff --git a/chrome/common/extensions/docs/examples/extensions/native_messaging/echo.rb b/chrome/common/extensions/docs/examples/extensions/native_messaging/echo.rb |
new file mode 100755 |
index 0000000000000000000000000000000000000000..079b73a5a8b31d452fc7e2d819a176330cea2894 |
--- /dev/null |
+++ b/chrome/common/extensions/docs/examples/extensions/native_messaging/echo.rb |
@@ -0,0 +1,40 @@ |
+#!/usr/bin/ruby |
+ |
+require 'iconv' |
+ |
+# A simple native client (works with Ruby 1.8 and 1.9). |
+# All this client does is echo the text it recieves back at the extension. |
+ |
+MESSAGE_TYPE_SEND_MESSAGE_REQUEST = 0 |
+MESSAGE_TYPE_SEND_MESSAGE_RESPONSE = 1 |
+MESSAGE_TYPE_CONNECT = 2 |
+MESSAGE_TYPE_CONNECT_MESSAGE = 3 |
+ |
+messageNumber = 0 |
+ |
+while (!STDIN.eof?) |
+ # Read the message type (first 4 bytes). |
+ messageType = STDIN.read(4).unpack('L')[0] |
+ |
+ # Read the message length (4 bytes). |
+ textLength = STDIN.read(4).unpack('L')[0] |
+ |
+ # Read the text (JSON object) of the message. |
+ text = Iconv.conv('UTF-8//IGNORE', 'UTF-8', |
+ STDIN.read(textLength) + ' ')[0..-2] |
+ |
+ messageNumber += 1 |
+ |
+ response = Iconv.conv( |
+ 'UTF-8//IGNORE', 'UTF-8', |
+ "{\"id\": #{messageNumber}, \"echo\": #{text}}" + ' ')[0..-2] |
+ |
+ # Choose the correct message type for the response. |
+ responseType = messageType == MESSAGE_TYPE_SEND_MESSAGE_REQUEST ? |
+ MESSAGE_TYPE_SEND_MESSAGE_RESPONSE : |
+ MESSAGE_TYPE_CONNECT_MESSAGE |
+ |
+ STDOUT.write([responseType, response.length].pack('LL')) |
+ STDOUT.write(response) |
+ STDOUT.flush() |
+end |