File: server.rb

package info (click to toggle)
ruby-async-io 1.34.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 424 kB
  • sloc: ruby: 3,103; makefile: 4
file content (43 lines) | stat: -rwxr-xr-x 794 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env ruby
# frozen_string_literal: true

$LOAD_PATH << File.expand_path("../../lib", __dir__)

require 'set'
require 'logger'

require 'async'
require 'async/reactor'
require 'async/io/host_endpoint'
require 'async/io/protocol/line'

class Server
	def initialize
		@connections = []
	end
	
	def run(endpoint)
		Async do |task|
			task.async do |subtask|
				while true
					subtask.sleep 10
					puts "Connection count: #{@connections.size}"
				end
			end
				
			
			endpoint.accept do |peer|
				stream = Async::IO::Stream.new(peer)
				
				@connections << stream
			end
		end
	end
end

Console.logger.level = Logger::INFO
Console.logger.info("Starting server...")
server = Server.new

endpoint = Async::IO::Endpoint.parse(ARGV.pop || "tcp://localhost:7234")
server.run(endpoint)