News : The level of daily SPAM has reached insane proportions, all registrations are now manual. I ask you to send me an e-mail (john (at) murga (dot) org), to confirm that you want me to create an account for you.


Post Reply  Post Thread 
the fltk loop and socket loop?
Author Message
jyf1987
Member
***


Posts: 58
Group: Registered
Joined: Jun 2008
Status: Offline
Reputation: 0
Post: #1
the fltk loop and socket loop?

as you know i want to make an simple irc client recently,
with your help i can draw the widget and do with the event
but i dont know how to make the fltk loop and socket loop together

this is my code ,it can display the msg from irc server
but it often freez(i mean do not work and leave a dead state)

Code:
dofile("cfg.ini")

function quit_callback(msg)
   if fltk.fl_choice("Are you sure you want to EXIT ?", "No", "Yes", nil) >= 1 then
      w:hide()
      os.exit()
   end
end

function iput(msg)
  rd:send("PRIVMSG "..cfg.chanel.." "..msg.."\n")
end

ww,wh,wt=500,480,"Simple IRC Client - Powered by jyf1987"

w= fltk:Fl_Window(ww,wh,wt)
w:callback(quit_callback)

msgout= fltk:Fl_Browser(5, 5, 490, 440)


msgin= fltk:Fl_Input(5, 450, 490, 25)
msgin:callback(
function()
  iput(msgin:value())
  msgout:bottomline(msgout:size())
  msgin:value("")
end
)
msgin:when(fltk.FL_WHEN_ENTER_KEY_ALWAYS)
w:show()
Fl:flush() -- these two lines are necessary to draw before run() is called
Fl:check()
w:make_current()
-----------------------utils-------------------------------

split=function(str, pat)
   local t = {} -- NOTE: use {n = 0} in Lua-5.0
   local fpat = "(.-)" .. pat
   local last_end = 1
   local s, e, cap = str:find(fpat, 1)
   while s do
      if s ~= 1 or cap ~= "" then
table.insert(t,cap)
      end
      last_end = e+1
      s, e, cap = str:find(fpat, last_end)
   end
   if last_end <= #str then
      cap = str:sub(last_end)
      table.insert(t, cap)
   end
   return t
end

function ipong(sock)
    sock:send('PONG 123456')
end

function iquit(sock)
    sock:send('QUIT')
    sock:close()
end


function main()
while s do
  local i=string.find(s,"PING")
  if i==1 then
    print(s)
    ipong(rd)
    s=rd:receive('*l')
  end
  
  local k=string.find(s,":",2)
  if k==nil then
    k=1
  end
  local headstr=string.sub(s,1,k-2)
  print(headstr)
  local header=split(headstr," ")
  print(table.getn(header))
  local j=string.find(headstr,"PRIVMSG")
  --print(j)
  if j then
    if string.find(headstr,cfg.nick,j+7) then
        if string.sub(s,string.find(s,":",2)+1,string.len(s))=="quit" then
            iquit(rd)
        else
            print("personal msg: "..s)
        end
    else
        print("group msg: "..s)
    end
  end
  s=rd:receive('*l')
  --Fl:wait()
  Fl:flush()
  Fl:check()
  murgaLua.sleep(700)
end

end

print=function(str)
  msgout:add(str)
  msgout:bottomline(msgout:size())
end
-------------------------my process------------------------
rd=socket.connect(cfg.host,cfg.port)
rd:send("USER "..cfg.user.." orrr2.cn "..cfg.host.." :"..cfg.user.."\n")
rd:send("NICK "..cfg.nick.."\n")
if (cfg.pwd and cfg.pwd~="") then
  rd:send("PRIVMSG /msg nickserv identify "..cfg.pwd.."\n")
end
rd:send("JOIN "..cfg.chanel.."\n")
s=rd:receive('*l')
main()


can someone show me anyother good method for doing this thing?


Cheers
jyf
09-16-2008 02:45 PM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

Messages In This Thread
the fltk loop and socket loop? - jyf1987 - 09-16-2008 02:45 PM

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites

Forum Jump: