Sinatra: Up and Running by Alan Harris & Konstantin Haase
Author:Alan Harris & Konstantin Haase [Alan Harris]
Language: eng
Format: epub, pdf
Tags: COMPUTERS / Programming Languages / Ruby
ISBN: 9781449304225
Publisher: O'Reilly Media
Published: 2011-11-22T05:00:00+00:00
Keeping the Connection Open
There are a number of applications that lend themselves to a persistent, open connection. Perhaps you’re developing a chat program or something similar; Example 2-37 shows how to create this type of connection and broadcast messages to subscribers.
Example 2-37. A simple streaming example
require 'sinatra' before do content_type :txt end connections = [] get '/consume' do stream(:keep_open) do |out| # store connection for later on connections << out # remove connection when closed properly out.callback { connections.delete(out) } # remove connection when closed due to an error out.errback do logger.warn 'we just lost a connection!' connections.delete(out) end end end get '/broadcast/:message' do connections.each do |out| out << "#{Time.now} -> #{params[:message]}" << "\n" end "Sent #{params[:message]} to all clients." end
Download
Sinatra: Up and Running by Alan Harris & Konstantin Haase.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9928)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9804)
The Mikado Method by Ola Ellnestam Daniel Brolund(9787)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8310)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7792)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7772)
Grails in Action by Glen Smith Peter Ledbrook(7705)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7567)
Windows APT Warfare by Sheng-Hao Ma(6950)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6684)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6551)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6425)
Kotlin in Action by Dmitry Jemerov(5074)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4323)
Solidity Programming Essentials by Ritesh Modi(4064)
Functional Programming in JavaScript by Mantyla Dan(4044)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3857)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3799)
The Ultimate iOS Interview Playbook by Avi Tsadok(3776)
