Skip to content

Instantly share code, notes, and snippets.

@EvanBrightside
Created November 27, 2018 23:17
Show Gist options
  • Select an option

  • Save EvanBrightside/fe35e12398d188bd3d6ed8ca5d438a83 to your computer and use it in GitHub Desktop.

Select an option

Save EvanBrightside/fe35e12398d188bd3d6ed8ca5d438a83 to your computer and use it in GitHub Desktop.

Revisions

  1. EvanBrightside renamed this gist Nov 27, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. EvanBrightside created this gist Nov 27, 2018.
    32 changes: 32 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    require 'socket'

    def welcome(chatter)
    chatter.print 'Welcome! Please enter your name: '
    chatter.readline.chomp
    end

    def broadcast(message, chatters)
    chatters.each do |chatter|
    chatter.puts message
    end
    end

    s = TCPServer.new(3939)
    chatters = []
    while (chatter = s.accept)
    Thread.new(chatter) do |c|
    name = welcome(chatter)
    broadcast("#{name} has joned", chatters)
    chatters << chatter
    begin
    loop do
    line = c.readline
    broadcast("#{name}: #{line}", chatters)
    end
    rescue EOFError
    c.close
    chatters.delete(c)
    broadcast("#{name} has left", chatters)
    end
    end
    end