您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页Ruby做Websocket Server 使用zlib压缩发送

Ruby做Websocket Server 使用zlib压缩发送

来源:二三娱乐

Ruby代码

require 'json'
require 'active_support/gzip'
require 'zlib'
require 'websocket-eventmachine-server'



h ={aaa: 'bbb' , ccc: 'ddd'}
string = JSON(h)

gzip_string = Zlib::Deflate.deflate(string)


EventMachine.run do
  WebSocket::EventMachine::Server.start(:host => "0.0.0.0", :port => 9999) do |ws|

    ws.onopen do
      puts "Client connected"
    end

    ws.onmessage do |msg, type|

      ws.send gzip_string , :type => :binary
    end

    ws.onclose do
      puts "Client disconnected"
    end

  end
end
var ws = new WebSocket('ws://localhost:9999')
ws.onopen = function(){
  ws.send('111'))
}

ws.onmessage = function(e) {
  var blob = e.data;
  var reader = new FileReader();
    reader.readAsBinaryString(blob);
    reader.onload = function (evt) {
      var data = pako.inflate(evt.target.result, { to: 'string' })
      console.log(JSON.parse(data))
    };
}

Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务