Module: JsClient

Includes:
EM::Protocols::LineText2
Defined in:
lib/jschat/client.rb

Defined Under Namespace

Modules: KeyboardInput Classes: TabComplete

Instance Method Summary collapse

Instance Method Details

#keyboard=(keyboard) ⇒ Object



238
239
240
# File 'lib/jschat/client.rb', line 238

def keyboard=(keyboard)
  @keyboard = keyboard
end

#namesObject



247
248
249
# File 'lib/jschat/client.rb', line 247

def names
  @names
end

#names=(names) ⇒ Object

This should take room into account



243
244
245
# File 'lib/jschat/client.rb', line 243

def names=(names)
  @names = names
end

#post_initObject



742
743
744
745
746
# File 'lib/jschat/client.rb', line 742

def post_init
  # When connected
  @protocol = JsChat::Protocol.new self
  send_identify ClientConfig[:name]
end

#receive_line(data) ⇒ Object



663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
# File 'lib/jschat/client.rb', line 663

def receive_line(data)
  data.split("\n").each do |line|
    json = JSON.parse(line.strip)
    # Execute the json
    protocol_response = @protocol.process_message json
    if protocol_response
      if protocol_response.kind_of? Array
        protocol_response.each do |response|
          @keyboard.show_message response.message, response.time
        end
      else
        @keyboard.show_message protocol_response.message, protocol_response.time
      end
    elsif json.has_key? 'notice'
      @keyboard.show_message "* #{json['notice']}"
    else
      @keyboard.show_message "* [SERVER] #{line}"
    end
  end
rescue Exception => exception
  @keyboard.show_message "* [CLIENT ERROR] #{exception}"
end

#send_change_name(username) ⇒ Object



727
728
729
730
731
732
733
# File 'lib/jschat/client.rb', line 727

def send_change_name(username)
  if @protocol.identified?
    send_data({ 'change' => 'user', 'user' => { 'name' => username } }.to_json + "\n")
  else
    send_identify username
  end
end

#send_identify(username) ⇒ Object



723
724
725
# File 'lib/jschat/client.rb', line 723

def send_identify(username)
  send_data({ 'identify' => username }.to_json + "\n")
end

#send_join(room) ⇒ Object



686
687
688
689
690
691
# File 'lib/jschat/client.rb', line 686

def send_join(room)
  @current_room = room 
  @keyboard.room_name = room 
  @keyboard.display_room_name
  send_data({ 'join' => room }.to_json + "\n")
end

#send_lastlog(room = nil) ⇒ Object



710
711
712
713
# File 'lib/jschat/client.rb', line 710

def send_lastlog(room = nil)
  room = @current_room if room.nil? or room.strip.empty?
  send_data({ 'lastlog' => room }.to_json + "\n")
end

#send_message(line) ⇒ Object



715
716
717
# File 'lib/jschat/client.rb', line 715

def send_message(line)
  send_data({ 'to' => @current_room, 'send' => line }.to_json + "\n")
end

#send_names(room = nil) ⇒ Object



705
706
707
708
# File 'lib/jschat/client.rb', line 705

def send_names(room = nil)
  room = @current_room if room.nil? or room.strip.empty?
  send_data({ 'names' => room }.to_json + "\n")
end

#send_part(room = nil) ⇒ Object



700
701
702
703
# File 'lib/jschat/client.rb', line 700

def send_part(room = nil)
  room = @current_room if room.nil?
  send_data({ 'part' => room }.to_json + "\n")
end

#send_private_message(user, message) ⇒ Object



719
720
721
# File 'lib/jschat/client.rb', line 719

def send_private_message(user, message)
  send_data({ 'to' => user, 'send' => message }.to_json + "\n")
end

#switch_room(room) ⇒ Object



693
694
695
696
697
698
# File 'lib/jschat/client.rb', line 693

def switch_room(room)
  @current_room = room
  @keyboard.room_name = room 
  @keyboard.display_room_name
  @keyboard.show_message "* Switched room to: #{room}"
end

#unbindObject



735
736
737
738
739
740
# File 'lib/jschat/client.rb', line 735

def unbind
  Ncurses.endwin
  Ncurses.clear
  puts "Disconnected from server"
  exit
end