[SOLVED]Server and Clients online text chat

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
Nero
Veteran
Posts: 248
Joined: Tue Aug 09, 2016 2:59 pm
Contact:

[SOLVED]Server and Clients online text chat

#1 Post by Nero »

I'm trying to make very basic text chat between renpy client and server I have coded using python language.

Are we able to run renpy.input() and while loop in same time? Idea is that while loop would constantly be getting data from the server and input would be used to send data to back server whenever we type something - so server can send this data to other clients.

It looks like renpy.input() completely freezes the game and no data can be sent or received while in input screen.

My code works but issue is that it's getting updates from server only after I write something in input command, I want to have input console up all time and having while loop running in background at the same time. Does Ren'Py allow this?


Here is my server code:
Note you have to run this outside of Ren'py in something like Pycharm

Code: Select all

import socket
import threading

def handle_client(client_socket, address, clients):
    try:
        # Notify the client of successful connection
        welcome_message = 'Welcome to the chat server! Type "bye" to exit.'
        client_socket.sendall(welcome_message.encode('utf-8'))

        while True:
            # Receive data from the client
            data = client_socket.recv(1024)
            if not data:
                break

            message = data.decode('utf-8')
            print('Received from {}: {}'.format(address, message))

            # Broadcast the message to all other clients
            for other_client, _ in clients:
                if other_client != client_socket:
                    try:
                        other_client.sendall('{}: {}'.format(address, message).encode('utf-8'))
                    except socket.error:
                        # Remove disconnected clients
                        clients.remove((other_client, _))

            if message.lower() == 'bye':
                break

    finally:
        # Clean up the connection
        client_socket.close()
        print('Connection with {} closed.'.format(address))

# Create a socket object
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('localhost', 8888)

server_socket.bind(server_address)
server_socket.listen(5)

print('Server listening on {}:{}'.format(*server_address))

clients = []

while True:
    client_socket, client_address = server_socket.accept()
    print('Connection from', client_address)

    # Add the new client to the list
    clients.append((client_socket, client_address))

    # Start a new thread to handle the client
    client_thread = threading.Thread(target=handle_client, args=(client_socket, client_address, clients))
    client_thread.start()

And this is my client (code used in Ren'Py)

Code: Select all

screen display_text():
    text "%s" % server.data



init python:
    import socket
    import threading

    class Server(object):
        data = "Test"    
    server = Server()

    def start_program():

        def receive_messages(client_socket):
            while True:
                data = client_socket.recv(1024)
                if not data:
                    break
                print(data.decode('utf-8'))
                server.data = data.decode('utf-8')

        # Create a socket object
        client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server_address = ('localhost', 8888)

        client_socket.connect(server_address)

        # Start a thread to receive messages from the server
        receive_thread = threading.Thread(target=receive_messages, args=(client_socket,))
        receive_thread.start()

        try:
            while True:
                # Send a message to the server
                message = renpy.input('Your message: ')
                client_socket.sendall(message.encode('utf-8'))
                if message.lower() == 'bye':
                    break

        finally:
            # Clean up the connection
            client_socket.close()

label start:
    "Start Game"
    show screen display_text
    "Showing Screen"
    $ start_program()
Last edited by Nero on Mon Jan 01, 2024 9:29 am, edited 1 time in total.

User avatar
m_from_space
Miko-Class Veteran
Posts: 978
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Server and Clients online text chat

#2 Post by m_from_space »

Nero wrote: Sun Dec 31, 2023 8:31 pm Are we able to run renpy.input() and while loop in same time?
If I understand you correctly, you mean that for example you are a user inside the "client" and you are going to type something. And while you are doing that, in the background there should be changes visible, that were coming in from the server (like a live chat update)?

I would think that you can just use a screen with a repeating timer that continuously executes a function in the background (checking for server data). But you shouldn't call that function too often (maybe once every second, definitely not 0.01 seconds, because that will "freeze" the rest of your game).

Code: Select all

init python:
    def checkServer():
        ...

screen myscreen():
    timer 1.0 action Function(checkServer) repeat True

label start:
    show screen myscreen
    "What's up?"
    $ message = renpy.input()
    ...

Nero
Veteran
Posts: 248
Joined: Tue Aug 09, 2016 2:59 pm
Contact:

Re: Server and Clients online text chat

#3 Post by Nero »

Yes you got it right, nice idea I just tested it and it works. Thanks for the help!

Post Reply

Who is online

Users browsing this forum: Sirifys-Al, Zapor