jaraco / irc

Full-featured Python IRC library for Python.
MIT License
390 stars 84 forks source link

Example for creating multiple connections to a server? #206

Closed OrpheusGr closed 1 year ago

OrpheusGr commented 1 year ago

I'm probably not doing something right, but i'm currently trying to make a test script that uses the Reactor class to make more than 1 connections to the same server. I'd be very greatful if you could show me an example, cause my so far attempts either connect only the first client or they stop the first client from connecting and "change servers".

This is my last attempt:

import irc.client

def on_connect(connection, event):
    connection.join("#chaos")

def on_join(connection, event):
    print(event.source, "joined!")

def on_disconnect(connectiom, event):
    print("Disconnected")

def startirc(server, port, nick):
    s = irc.client.Reactor()
    x = s.server()
    c = x.connect(server, port, nick)
    c.add_global_handler("welcome", on_connect)
    c.add_global_handler("join", on_join)
    c.add_global_handler("disconnect", on_disconnect)
    s.process_forever()

My logic is that i would call startirc to create a new IRC connection each time that i needed it.

Thanks in advance!

OrpheusGr commented 1 year ago

I've figured this out! Here's two working examples:

A single reactor object with multiple connections that uses process_forever(). The dissadvantage i found in this is that any event triggers for all ServerConnection objects.

global reactlist
reactlist = []
global last_call
last_call = 0
def makereactor():
    s  = irc.client.Reactor()
    return s

def startirc():
    global react
    nicks = ["test1", "test2"]
    react = makereactor()
    for i in range(len(nicks)):
        c = react.server().connect("some.server.somewhere", 6667,
 nicks[i])
    c.add_global_handler("welcome", on_connect)
    c.add_global_handler("join", on_join)
    c.add_global_handler("disconnect", on_disconnect)
    c.add_global_handler("privmsg", on_pm)
    while True:
        react.process_once()

One reactor object for each new client we want to make, and we call process_once() on every reactor object by adding them to a list and iterating over them, i suppose i could also add new reactor objects to the list whenever i need to with a seperate function but i haven't tried yet.

def process_all():
     global reactlist
     global last_call
     if last_call == 0:
         last_call == time.time()
     if time.time() < last_call + 10:
         return
     for i in range(len(reactlist)):
          reactlist[i].process_once()

def startirct():
    global reactlist
    nicks = ["test1", "test2"]
    for i in range(len(nicks)):
        react = makereactor()
        reactlist.append(react)                                 
        c = react.server().connect("some.server.somewhere", 6667,
 nicks[i])
        c.add_global_handler("welcome", on_connect)
        c.add_global_handler("disconnect", on_disconnect
)
        c.add_global_handler("privmsg", on_pm)
    while True:
        process_all()