Ruby EventMachine聊天服务器的未初始化常量错误
我正在尝试使用EventManager在ruby中构建一个聊天服务器。不用说,我对Ruby还不熟悉,对当前遇到的错误有点不知所措,因为我不知道这意味着什么,搜索也不会返回任何有价值的东西。这里是一些物流- (我只实现了登录和注册,所以我只包括这些…) 用户可以输入- 注册用户名密码-注册用户 登录用户名密码-登录用户 我接收用户发送的数据字符串,将其拆分为一个名为msg的数组,然后根据msg[0]对数据执行操作(作为命令,如REGISTER、LOGIN等) 这是我的代码,全部包含在一个文件中-chatserver.rb(解释如下):Ruby EventMachine聊天服务器的未初始化常量错误,ruby,events,chat,Ruby,Events,Chat,我正在尝试使用EventManager在ruby中构建一个聊天服务器。不用说,我对Ruby还不熟悉,对当前遇到的错误有点不知所措,因为我不知道这意味着什么,搜索也不会返回任何有价值的东西。这里是一些物流- (我只实现了登录和注册,所以我只包括这些…) 用户可以输入- 注册用户名密码-注册用户 登录用户名密码-登录用户 我接收用户发送的数据字符串,将其拆分为一个名为msg的数组,然后根据msg[0]对数据执行操作(作为命令,如REGISTER、LOGIN等) 这是我的代码,全部包含在一个文件中-c
需要“rubygems”
需要“eventmachine”
类服务器
属性访问器:客户端,:通道,:userCreds,:userChannels
def初始化
@客户端=[]#连接的客户端列表,例如[192.168.1.2、192.168.1.3]
@用户={}#已登录用户列表,例如[汤姆、萨姆、杰瑞]
@通道=[]#通道列表,例如[a、b、c]
@userCreds={}#用户凭证散列,例如{tom:password1、sam:password2等}
@UserChannels={}#用户及其频道,例如{tom:a,sam:a,jerry:b}
结束
def启动
@signature=EventMachine.start_服务器(“127.0.0.1”,3200,客户端)do|con|
con.server=self
结束
结束
def停止
EventMachine.stop_服务器(@signature)
除非等待连接和停止
EventMachine.add_periodic.timer(1){wait_for_connections_and_stop}
结束
结束
#用户名是否已经存在?
def具有_用户名?(名称)
@userCreds.has_key?(名称)
结束
#用户是否已登录?
def已登录?(名称)
如果@users[name]==1
真的
其他的
假的
结束
结束
#用户是否输入了正确的pwd?
def正确通过?(通过)
如果@userCreds[name]==pass
真的
其他的
假的
结束
结束
私有的
def等待连接和停止
如果@clients.empty?
停止
真的
其他的
将“正在等待#{@clients.size}客户端停止”
假的
结束
结束
结束
类连接
哦,好吧,这只是开始,所以没那么复杂。由于我是Ruby新手,我有一种感觉,就是我没有正确地声明变量或使用范围。以下是错误输出:
rb:16:instart”:未初始化的常量服务器::客户端
(名称错误)来自chatserver.rb:110:in
block in'from
/Users/meth/.rvm/gems/ruby-1.9.3-p392@rails3tutorial2ndEd/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in
call'来自
/Users/meth/.rvm/gems/ruby-1.9.3-p392@rails3tutorial2ndEd/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in
从运行机器
/Users/meth/.rvm/gems/ruby-1.9.3-p392@rails3tutorial2ndEd/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in
从chatserver.rb:108:in运行
忽略最后一行中main中的斜杠。
第108行是最后一个函数-EventMachine::run do etc
如果我没有提供足够的信息,请随时通知我。我想当您调用
EventMachine::start\u server
时,您需要将您的连接类作为处理程序提供给它<代码>客户端
未在任何位置定义
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 and their channels e.g. { tom: a, sam: a, jerry: b }
end
def start
@signature = EventMachine.start_server("127.0.0.1", 3200, Client) do |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
# Does the username already exist?
def has_username?(name)
@userCreds.has_key?(name)
end
# Is the user already logged in?
def logged_in?(name)
if @users[name] == 1
true
else
false
end
end
# Did the user enter the 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 for #{@clients.size} client(s) to stop"
false
end
end
end
class Connection < EventMachine::Connection
attr_accessor :server, :name, :msg
def initialize
@name = nil
@msg = []
end
# First thing the user sees when they connect to the server.
def post_init
send_data("Welcome to the lobby.\nRegister or Login with REGISTER/LOGIN username password\nOr try HELP if you get stuck!")
end
# Start parsing incoming data
def receive_data(data)
data.strip!
msg = data.split("") #split data by spaces and throw it 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 to handle_register method
else
hanlde_login(msg) #send msg to handle_login method
end
end
def unbind
@server.clients.each { |client| client.send_data("#{@name} has just left") }
puts("#{@name} has just left")
@server.clients.delete(self)
end
private
def handle_register(msg)
if @server.has_username? msg[1] #user trying to register with a name that already exists?
send_data("That username is already taken! Choose another or login.")
return
else
@name = msg[1] #set name to username
@userCreds[name] = msg[2] #add username and password to user credentials hash
send_data("OK") #send user OK message
end
end
end
EventMachine::run do
s = Server.new
s.start #start server
puts "Server listening"
end