且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将我的 Kivy 客户端连接到服务器(TCP、套接字)

更新时间:2022-06-14 22:59:44

我有一个使用按钮的基本版本.无论是在本地机器上还是在线上.由于必须启动回复,因此该解决方案可能不适用于许多实时应用程序甚至聊天服务器.但是,对于我的多人纸牌游戏目标来说,只要有适当的条件就足够了.

I got a basic version of it working with buttons. Both on local machine and online. This Solution is likely not viable for many real time apps or even a chat server since the reply has to be initiated. However for my goal of a multiplayer card game it should more than suffice with proper conditionals.

本地机器测试视频

在视频中,我谈到了双击.我刚刚意识到这是因为第一次单击使窗口重新成为焦点.

In the video I talk about double clicking. I have just realized this is because the first click is putting the window back in focus.

编辑 2:在 kv 文件中使用 TextInput 而不是 Python 文件中的输入.

EDIT 2: Using TextInput in kv file instead of input in Python file.

服务器脚本:

import socket

def Main():
    host = '127.0.0.1'
    port = 7000

    mySocket = socket.socket()
    mySocket.bind((host,port))

    mySocket.listen(1)
    conn, addr = mySocket.accept()
    print ("Connection from: " + str(addr))
    message = 'Thank you connecting'
    conn.send(message.encode())

    while True:
        data = conn.recv(1024).decode()
        strdata = str(data)
        print(strdata)
        reply = 'confirmed'
        conn.send(reply.encode())

    mySocket.close()

if __name__ == '__main__':
    Main()

这是一个非常简单的服务器.监听单个客户端,确认连接,打开发送和接收消息循环.

This is a pretty simple server. Listen for a single client, confirm connection, open a send and receive message loop.

这是一个并不复杂的客户端脚本:

This is the client script which isn't hugely complicated really:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
import socket

class BoxWidget(BoxLayout):
    s = socket.socket()
    host = '127.0.0.1'
    port = 7000
    display = ObjectProperty()


    def connect_to_server(self):
        # called by a Button press

        # Connects to the server
        self.s.connect((self.host, self.port)) 

        # Receives confirmation from Server
        data = self.s.recv(1024).decode()      

        # Converts confirmation to string
        strdata = str(data)                     

        # Prints confirmation
        print(strdata)                                   

    def send_message(self):    
        # Is called by the function below
        # Encodes and sends the message variable                  
        self.s.send(self.message.encode()) 

        # Waits for a reply   
        self.receive_message()                     

    def message_to_send(self):  
        # Defines Message to send                 
        self.message = self.display.text
        # Calls function to send the message                
        self.send_message()     

    # Note
    # When I used message = input directly in send_message,
    # the app would crash. So I defined message input 
    # in its own function which then calls the 
    # send function  

    # message_to_send is the function actually
    # called by a button press which then
    # starts the chain of events
    # Define Message, Send Message, get Reply

    def receive_message(self):
        # Decodes a reply                    
         reply = self.s.recv(1024).decode()

        # Converts reply to a str
        strreply = str(reply)

        # prints reply
        print(strreply)

class ServerApp(App):    
     def build(self):
          box = BoxWidget()
          return box

if __name__ == '__main__':
    ServerApp().run()    

忘记包含 kv 文件

<BoxWidget>:
     display: display
     Button:
        text: 'Hello'
        on_press: root.message_to_send()
    Button:
        text: 'Connect'
        on_press: root.connect_to_server()
    TextInput:
        id: display

在未来的迭代中,我将用条件语句替换打印语句(即客户端 1 是否抽了牌?如果是,客户端 2 的对手抽了一张面朝下的牌等).

In future iterations, I'll be replacing print statements with conditionals (ie did client one draw a card? if so client 2's opponent draws a face-down card etc).

现在相对初级,但您可以从这里做很多事情.

Relatively rudimentary as it is now but there is a lot you could do from here.