diff options
| author | mokha <mokha@cisco.com> | 2019-02-08 16:30:51 -0700 |
|---|---|---|
| committer | mokha <mokha@cisco.com> | 2019-02-08 16:30:51 -0700 |
| commit | e8688ac5d6bc644f31572c0655d0e3ad657a1a1e (patch) | |
| tree | c93d9f7fdb36cda906e3b6b17a27f7cd6e944a0e | |
create hijack example app.
| -rw-r--r-- | Gemfile | 11 | ||||
| -rw-r--r-- | Gemfile.lock | 17 | ||||
| -rwxr-xr-x | caller.sh | 10 | ||||
| -rw-r--r-- | config.ru | 54 | ||||
| -rw-r--r-- | publisher.rb | 5 |
5 files changed, 97 insertions, 0 deletions
@@ -0,0 +1,11 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } + +# gem "rails" + +gem "rack", "~> 2.0" +gem 'puma' +gem 'redis' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..c33a7c1 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,17 @@ +GEM + remote: https://rubygems.org/ + specs: + puma (3.12.0) + rack (2.0.6) + redis (4.1.0) + +PLATFORMS + ruby + +DEPENDENCIES + puma + rack (~> 2.0) + redis + +BUNDLED WITH + 2.0.1 diff --git a/caller.sh b/caller.sh new file mode 100755 index 0000000..8947896 --- /dev/null +++ b/caller.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +curl -i \ + --include \ + --no-buffer \ + --header "Connection: Upgrade" \ + --header "Upgrade: websocket" \ + --header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" \ + --header "Sec-WebSocket-Version: 13" \ + http://localhost:9292/ diff --git a/config.ru b/config.ru new file mode 100644 index 0000000..160f033 --- /dev/null +++ b/config.ru @@ -0,0 +1,54 @@ +#app = lambda do |env| +#io = env['rack.hijack_io'] +#begin +#io.write("Status: 200\r\n") +#io.write("Connection: close\r\n") +#io.write("Content-Type: text/plain\r\n") +#io.write("\r\n") +#10.times do |i| +#io.write("Line #{i + 1}!\n") +#io.flush +#sleep 1 +#end +#ensure +#io.close +#end +#end + +#run app + +require 'rack' +require 'puma' +require 'rack/handler/puma' +require 'redis' + +class Application + attr_reader :redis + + def initialize + @redis = Redis.new + end + + def call(env) + if env['rack.hijack?'] + env['rack.hijack'].call + io = env['rack.hijack_io'] + redis.subscribe("ruby") do |on| + on.message do |channel, message| + puts message.inspect + io.write(message) + end + end + #Thread.new do + #loop do + #sleep 5 + #io.write("I'm awake...") + #end + #end + else + ['200', {'Content-Type' => 'text/html'}, ['A barebones rack app.']] + end + end +end + +Rack::Handler::Puma.run Application.new diff --git a/publisher.rb b/publisher.rb new file mode 100644 index 0000000..852edbf --- /dev/null +++ b/publisher.rb @@ -0,0 +1,5 @@ + +require 'redis' + +redis = Redis.new +redis.publish("ruby", "I love ruby") |
