From 8c727151c5baee71b6efe6e21e11e5d8c832d779 Mon Sep 17 00:00:00 2001 From: Ian Pedras Date: Wed, 10 Jul 2024 21:27:11 +0100 Subject: [PATCH] v0.0.1 --- README.md | 49 ---------------- cli.png | Bin 0 -> 2358 bytes client-config.json | 5 -- tcp-client.rb => connector.rb | 73 ++++++++++-------------- main.glade | 104 ++++++++++++++++++++++++++++++++++ server-config.json | 4 -- tcp-server.rb | 55 ------------------ ui.rb | 83 +++++++++++++++++++++++++++ 8 files changed, 217 insertions(+), 156 deletions(-) delete mode 100644 README.md create mode 100644 cli.png delete mode 100644 client-config.json rename tcp-client.rb => connector.rb (51%) mode change 100644 => 100755 create mode 100644 main.glade delete mode 100644 server-config.json delete mode 100644 tcp-server.rb create mode 100755 ui.rb diff --git a/README.md b/README.md deleted file mode 100644 index 5312ccf..0000000 --- a/README.md +++ /dev/null @@ -1,49 +0,0 @@ -# TCP Talk -Talk app that uses tcp in the backend - -## Features -1. Simple Chat -You can chat, simplelest feature - -2. Color coding -Send messages with colors -`Hello World.green` the `.green` puts the text in green - -3. Config file for server and client -You can configure your server and your client with two JSON files. - -## More Info - -### Color Coding -Sending messages with colors works like this: -`Hello World.green` the `.green` puts the text in green - -Other avalible colors are: - **color** ascii color code - **black**: 30 - **red**: 31 - **green**: 32 - **yellow**: 33 - **blue**: 34 - **magenta**: 35 - **cyan**: 36 - **white**: 37 - -### Server Config -The server configuration file `server-config.json` is a JSON file to configure you server: -``` -{ - "server_name": "tcp-test-server", - "port": 4000 -} -``` -### Client Config -The client configuration file `client-config.json` is a JSON file to configure you client: -``` -{ - "hostname": "localhost", - "port": 4000, - "username": "user" -} - -``` \ No newline at end of file diff --git a/cli.png b/cli.png new file mode 100644 index 0000000000000000000000000000000000000000..065e10a3570cbd34d24200896bb5a8a937750715 GIT binary patch literal 2358 zcmeAS@N?(olHy`uVBq!ia0y~yV6tIgV65O^1BwLbDc1ry&6&>50iMpz3I#>^X_+~x z3=A3*YbV-z9Cna78XvthXjN01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100644 index b0c52d5..0000000 --- a/client-config.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "hostname": "localhost", - "port": 4000, - "username": "tcp-user" -} diff --git a/tcp-client.rb b/connector.rb old mode 100644 new mode 100755 similarity index 51% rename from tcp-client.rb rename to connector.rb index 1d6dd5c..0480ac6 --- a/tcp-client.rb +++ b/connector.rb @@ -1,5 +1,5 @@ -require 'socket' require 'json' +require 'socket' # Get colors working class String @@ -24,6 +24,7 @@ class String default_hostname = 'localhost' default_port = 4000 default_username = 'default_user' +server= '(Server)' begin # Attempt to read and parse the configuration file @@ -32,7 +33,7 @@ class String # Extract configuration values hostname = config['hostname'] port = config['port'] - username = config['username'] + @my_username = config['username'] rescue Errno::ENOENT, JSON::ParserError # If the file is not found or there's a JSON parsing error, use default values puts "(Client) Error reading configuration file. Using default values.".yellow @@ -45,51 +46,37 @@ class String puts "You can use 'config.json' if you want to preconfigure this" end -begin - socket = TCPSocket.open(hostname, port) - puts "(Client) Connected to server at --> #{hostname}:#{port}".yellow - - -rescue Errno::ECONNREFUSED - puts "(Client) Error: Connection refused. Please ensure the server is running and try again. Host Server:#{hostname} Host Server Port:#{port}".yellow - exit -end - -# Handle Ctrl+C (SIGINT) to gracefully shut down the client -trap("INT") do - puts "\n (Client) Disconnecting from server...".yellow - socket.puts "exit" rescue nil - socket.close - exit -end - -Thread.new do - loop do - msg = socket.gets - if msg.nil? - puts "(Client) Connection closed by server.".yellow - exit - else - msg.chomp! +# Start the connection +def start_connection(hostname, port, username, text_view) + begin + socket = TCPSocket.open(hostname, port) + puts "(Client) Connected to server at --> #{hostname}:#{port}".yellow - # Detect and apply color - if msg.match(/\.\w+$/) - color_code = msg.match(/\.(\w+)$/)[1] - if String::COLORS.key?(color_code.to_sym) - colored_msg = msg.gsub(/\.\w+$/, '').send(color_code.to_sym) - puts colored_msg + Thread.new do + loop do + msg = socket.gets + if msg.nil? + puts "(Client) Connection closed by server.".yellow + exit else - puts msg + msg.chomp! + + # Update the text buffer of the text_view on the GTK main thread + GLib::Idle.add do + buffer = text_view.buffer + iter = buffer.end_iter + buffer.insert(iter, "#{@my_username}: #{msg}" + "\n") + text_view.scroll_to_iter(iter, 0, false, 0, 1) + false + end end - else - puts msg end end - end -end -loop do - msg = $stdin.gets.chomp - break if msg == "exit" - socket.puts "#{username}:#{msg}" + + return socket + rescue Errno::ECONNREFUSED + puts "(Client) Error: Connection refused. Please ensure the server is running and try again. Host Server:#{hostname} Host Server Port:#{port}".yellow + exit + end end diff --git a/main.glade b/main.glade new file mode 100644 index 0000000..b3d6fec --- /dev/null +++ b/main.glade @@ -0,0 +1,104 @@ + + + + + + False + TCP Talk Client + + + + True + False + + + + True + False + 23 + + + Send + True + True + True + + + + 2 + 3 + + + + + True + True + + + + 1 + 3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1 + 1 + + + + + True + True + + + 1 + 0 + + + + + + + + + + + + + + + + + + diff --git a/server-config.json b/server-config.json deleted file mode 100644 index 800ec30..0000000 --- a/server-config.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "server_name": "tcp-test-server", // warning, may not work - "port": 4000 -} diff --git a/tcp-server.rb b/tcp-server.rb deleted file mode 100644 index b722369..0000000 --- a/tcp-server.rb +++ /dev/null @@ -1,55 +0,0 @@ -require 'socket' -require 'json' - - -# Attempt to read and parse the configuration file -config = JSON.parse(File.read('server-config.json')) -# Extract configuration values -$server_name = config['servername'] -PORT = config['port'] - - -# Initialize the server and client list -server = TCPServer.open(PORT) -clients = [] - - -puts "Server started on port #{PORT}..." - -loop do - client = server.accept - clients << client - puts "Client connected." - - - Thread.start(client) do |client_conn| - begin - - srv_name = "TCP Test Server" - client_conn.puts "Hello, welcome to #{srv_name}" - - - loop do - msg = client_conn.gets&.chomp - break if msg.nil? || msg == "exit" - - clients.each do |c| - begin - c.puts msg unless c == client_conn - rescue Errno::EPIPE, IOError => e - puts "Error: #{e.message}. Removing client." - clients.delete(c) - c.close - end - end - end - rescue Errno::EPIPE, IOError => e - puts "Error: #{e.message}. Client disconnected unexpectedly." - ensure - clients.delete(client_conn) - client_conn.puts "[TCP Server] Server shut down!" rescue nil - client_conn.close - puts "Client disconnected." - end - end -end diff --git a/ui.rb b/ui.rb new file mode 100755 index 0000000..cf043e4 --- /dev/null +++ b/ui.rb @@ -0,0 +1,83 @@ +require 'gtk3' +require_relative 'connector' + + + + +class MyWindow + def initialize + # Load the .glade file + builder = Gtk::Builder.new +glade_file = File.expand_path('main.glade', ENV['HERE']) + puts "Resolved path to main.glade: #{glade_file}" + + builder.add_from_file(glade_file) + + # Get the main window object + @window = builder.get_object('main_window') + unless @window + puts "Error: Could not find the main_window object in the Glade file." + exit + end + @window.signal_connect('destroy') { Gtk.main_quit } + + # Get the GtkEntry object + @entry = builder.get_object('message_box') + unless @entry + puts "Error: Could not find the message_box object in the Glade file." + exit + end + @entry.signal_connect('activate') { on_entry_activate } + + # Get the GtkButton object and connect its clicked signal + button = builder.get_object('send_message') + unless button + puts "Error: Could not find the send_message object in the Glade file." + exit + end + button.signal_connect('clicked') { on_button_clicked } + + # Get the GtkTextView object for displaying messages + @message_view = builder.get_object('messages') + unless @message_view + puts "Error: Could not find the messages object in the Glade file." + exit + end + + # Show all the widgets + @window.show_all + + # Initialize connection and pass the message text view + @socket = start_connection('localhost', 4000, 'default_user', @message_view) + end + + def on_entry_activate + send_message + end + + def on_button_clicked + send_message + end + + def send_message + # Retrieve the text from the GtkEntry widget + entry_text = @entry.text + return if entry_text.strip.empty? + + puts "User inputted a message: #{entry_text}" + # Send the message through the socket + @socket.puts entry_text + # Clear the GtkEntry widget + @entry.text = '' + + # Append the sent message to the GtkTextView + buffer = @message_view.buffer + iter = buffer.end_iter + buffer.insert(iter, "You: #{entry_text}\n") + @message_view.scroll_to_iter(iter, 0, false, 0, 1) + end +end + +# Start the GTK main loop +MyWindow.new +Gtk.main pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy