OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/ruby |
| 2 |
| 3 require 'iconv' |
| 4 |
| 5 # A simple native client (works with Ruby 1.8 and 1.9). |
| 6 # All this client does is echo the text it recieves back at the extension. |
| 7 |
| 8 MESSAGE_TYPE_SEND_MESSAGE_REQUEST = 0 |
| 9 MESSAGE_TYPE_SEND_MESSAGE_RESPONSE = 1 |
| 10 MESSAGE_TYPE_CONNECT = 2 |
| 11 MESSAGE_TYPE_CONNECT_MESSAGE = 3 |
| 12 |
| 13 messageNumber = 0 |
| 14 |
| 15 while (!STDIN.eof?) |
| 16 # Read the message type (first 4 bytes). |
| 17 messageType = STDIN.read(4).unpack('L')[0] |
| 18 |
| 19 # Read the message length (4 bytes). |
| 20 textLength = STDIN.read(4).unpack('L')[0] |
| 21 |
| 22 # Read the text (JSON object) of the message. |
| 23 text = Iconv.conv('UTF-8//IGNORE', 'UTF-8', |
| 24 STDIN.read(textLength) + ' ')[0..-2] |
| 25 |
| 26 messageNumber += 1 |
| 27 |
| 28 response = Iconv.conv( |
| 29 'UTF-8//IGNORE', 'UTF-8', |
| 30 "{\"id\": #{messageNumber}, \"echo\": #{text}}" + ' ')[0..-2] |
| 31 |
| 32 # Choose the correct message type for the response. |
| 33 responseType = messageType == MESSAGE_TYPE_SEND_MESSAGE_REQUEST ? |
| 34 MESSAGE_TYPE_SEND_MESSAGE_RESPONSE : |
| 35 MESSAGE_TYPE_CONNECT_MESSAGE |
| 36 |
| 37 STDOUT.write([responseType, response.length].pack('LL')) |
| 38 STDOUT.write(response) |
| 39 STDOUT.flush() |
| 40 end |
OLD | NEW |