events - Uninitialized Constant Error from Ruby EventMachine Chat Server -
i'm trying build chat server in ruby using eventmanager. needless day, i'm new ruby , feeling little on head current error getting, have no clue means , search doesn't return valuable. here's of logistics-
(ive implemented login , register i'll include those..) user can enter- register username password - registers user login username password - logins user
i'm taking in string of data user sends, splitting array called msg, , acting on data based on msg[0] (as command, register, login, etc)
here code, contained in single file- chatserver.rb (explanation follows):
require 'rubygems' require 'eventmachine' class server attr_accessor :clients, :channels, :usercreds, :userchannels def initialize @clients = [] #list of clients connected e.g. [192.168.1.2, 192.168.1.3] @users = {} #list of users 'logged in' e.g. [tom, sam, jerry] @channels = [] #list of channels e.g. [a, b, c] @usercreds = {} #user credentials hash e.g. { tom: password1, sam: password2, etc } @userchanels = {} #users , channels e.g. { tom: a, sam: a, jerry: b } end def start @signature = eventmachine.start_server("127.0.0.1", 3200, client) |con| con.server = self end end def stop eventmachine.stop_server(@signature) unless wait_for_connections_and_stop eventmachine.add_periodic.timer(1) { wait_for_connections_and_stop } end end # username exist? def has_username?(name) @usercreds.has_key?(name) end # user logged in? def logged_in?(name) if @users[name] == 1 true else false end end # did user enter correct pwd? def correct_pass?(pass) if @usercreds[name] == pass true else false end end private def wait_for_connections_and_stop if @clients.empty? eventmachine.stop true else puts "waiting #{@clients.size} client(s) stop" false end end end class connection < eventmachine::connection attr_accessor :server, :name, :msg def initialize @name = nil @msg = [] end # first thing user sees when connect server. def post_init send_data("welcome lobby.\nregister or login register/login username password\nor try if stuck!") end # start parsing incoming data def receive_data(data) data.strip! msg = data.split("") #split data spaces , throw in array msg[] if data.empty? #the user entered nothing? send_data("you didn't type anything! try help.") return elsif msg[0] == "register" handle_register(msg) #send msg handle_register method else hanlde_login(msg) #send msg handle_login method end end def unbind @server.clients.each { |client| client.send_data("#{@name} has left") } puts("#{@name} has left") @server.clients.delete(self) end private def handle_register(msg) if @server.has_username? msg[1] #user trying register name exists? send_data("that username taken! choose or login.") return else @name = msg[1] #set name username @usercreds[name] = msg[2] #add username , password user credentials hash send_data("ok") #send user ok message end end end eventmachine::run s = server.new s.start #start server puts "server listening" end
whew, okay, it's beginning, not complicated. since i'm new ruby have feeling i'm not declaring variable or using scope correctly. here's error output:
chatserver.rb:16:in
start': uninitialized constant server::client (nameerror) chatserver.rb:110:in
block in ' /users/meth/.rvm/gems/ruby-1.9.3-p392@rails3tutorial2nded/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:incall' /users/meth/.rvm/gems/ruby-1.9.3-p392@rails3tutorial2nded/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in
run_machine' /users/meth/.rvm/gems/ruby-1.9.3-p392@rails3tutorial2nded/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:inrun' chatserver.rb:108:in
<\main>'
ignore slash in main in last line. line 108 last function- eventmachine::run etc.
any appreciated, if didn't provide enough info let me know.
i think when call eventmachine::start_server
need give connection
class handler. client
not defined anywhere.
Comments
Post a Comment