Sha256: 48324ceb15f1fac83e4f5177e712b28c6db500c3156184b760f434fa388f8ace

Contents?: true

Size: 651 Bytes

Versions: 19

Compression:

Stored size: 651 Bytes

Contents

# frozen_string_literal: true

require 'bundler/setup'
require 'tipi'
require 'tipi/websocket'

def ws_handler(conn)
  timer = spin_loop(interval: 1) do
    conn << Time.now.to_s
  end
  while (msg = conn.recv)
    conn << "you said: #{msg}"
  end
ensure
  timer.stop
end

opts = {
  reuse_addr:  true,
  dont_linger: true,
}

HTML = IO.read(File.join(__dir__, 'ws_page.html'))

puts "pid: #{Process.pid}"
puts 'Listening on port 4411...'

Tipi.serve('0.0.0.0', 4411, opts) do |req|
  if req.upgrade_protocol == 'websocket'
    conn = req.upgrade_to_websocket
    ws_handler(conn)
  else
    req.respond(HTML, 'Content-Type' => 'text/html')
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
tipi-0.55 examples/http_request_ws_server.rb
tipi-0.54 examples/http_request_ws_server.rb
tipi-0.53 examples/http_request_ws_server.rb
tipi-0.52 examples/http_request_ws_server.rb
tipi-0.51 examples/http_request_ws_server.rb
tipi-0.50 examples/http_request_ws_server.rb
tipi-0.49 examples/http_request_ws_server.rb
tipi-0.47 examples/http_request_ws_server.rb
tipi-0.46 examples/http_request_ws_server.rb
tipi-0.45 examples/http_request_ws_server.rb
tipi-0.43 examples/http_request_ws_server.rb
tipi-0.42 examples/http_request_ws_server.rb
tipi-0.41 examples/http_request_ws_server.rb
tipi-0.40 examples/http_request_ws_server.rb
tipi-0.39 examples/http_request_ws_server.rb
tipi-0.38 examples/http_request_ws_server.rb
tipi-0.37.2 examples/http_request_ws_server.rb
tipi-0.37.1 examples/http_request_ws_server.rb
tipi-0.37 examples/http_request_ws_server.rb