add a callback for mysql auth

This commit is contained in:
fztcjjl
2015-10-24 06:37:25 +08:00
parent fe3954fb04
commit 1653b3213a
2 changed files with 11 additions and 7 deletions

View File

@@ -418,7 +418,7 @@ local function _recv_auth_resp(self)
end end
local function _mysql_login(self,user,password,database,cb) local function _mysql_login(self,user,password,database,on_connect)
return function(sockchannel) return function(sockchannel)
local packet, typ, err = sockchannel:response( _recv_decode_packet_resp(self) ) local packet, typ, err = sockchannel:response( _recv_decode_packet_resp(self) )
@@ -491,8 +491,8 @@ local function _mysql_login(self,user,password,database,cb)
local authpacket=_compose_packet(self,req,packet_len) local authpacket=_compose_packet(self,req,packet_len)
sockchannel:request(authpacket,_recv_auth_resp(self)) sockchannel:request(authpacket,_recv_auth_resp(self))
if cb then if on_connect then
cb(self) on_connect(self)
end end
end end
end end
@@ -629,7 +629,7 @@ local function _query_resp(self)
end end
end end
function _M.connect(opts, cb) function _M.connect(opts)
local self = setmetatable( {}, mt) local self = setmetatable( {}, mt)
@@ -648,7 +648,7 @@ function _M.connect(opts, cb)
local channel = socketchannel.channel { local channel = socketchannel.channel {
host = opts.host, host = opts.host,
port = opts.port or 3306, port = opts.port or 3306,
auth = _mysql_login(self,user,password,database,cb), auth = _mysql_login(self,user,password,database,opts.on_connect),
} }
self.sockchannel = channel self.sockchannel = channel
-- try connect first only once -- try connect first only once

View File

@@ -70,14 +70,18 @@ local function test3( db)
end end
skynet.start(function() skynet.start(function()
local function on_connect(db)
db:query("set charset utf8");
end
local db=mysql.connect({ local db=mysql.connect({
host="127.0.0.1", host="127.0.0.1",
port=3306, port=3306,
database="skynet", database="skynet",
user="root", user="root",
password="1", password="1",
max_packet_size = 1024 * 1024 max_packet_size = 1024 * 1024,
}, function(db) db:query("set charset utf8") end) on_connect = on_connect
})
if not db then if not db then
print("failed to connect") print("failed to connect")
end end