Skip to content

Commit

Permalink
Update ChatClient.py
Browse files Browse the repository at this point in the history
  • Loading branch information
MuRongPIG authored Sep 25, 2021
1 parent 87d7931 commit b0a4e08
Showing 1 changed file with 70 additions and 43 deletions.
113 changes: 70 additions & 43 deletions ChatClient.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import ast
import json
# https://api.github.com/repos/MuRongPIG/CrossStreet-python/releases/latest?access_token=52f288af346824eb88c5db1afb8b3a8349772363
import os
from ws4py.client.threadedclient import WebSocketClient
import platform
import emoji
import requests

from tkinter import *
from tkinter import messagebox
from emoji import demojize
import requests

from ws4py.client.threadedclient import WebSocketClient

connected = False
channel_ = '公共聊天室'
Expand All @@ -17,11 +18,6 @@
client_name = '[PIG Chat](https://github.com/MuRongPIG/CrossStreet-Python)'
client_key = 'HXjURea2vReYejA'

def var_append(var, data):
# var.set(var.get() + '\n' + data)
my_text.insert(INSERT, '\n' + data)
my_text.see(END)


class Settings:
def __init__(self):
Expand Down Expand Up @@ -76,13 +72,14 @@ def parse_cmds(data):
return "%s 加入聊天室" % data['nick']

if cmd == 'chat':
if 'nick' in data and 'trip' in data:
if 'trip' in data:
return "%s %s\n%s" % (data['trip'], data['nick'], data['text'])
return "%s %s\n%s" % (m_settings.username, my_trip, data['text'])
return "%s %s\n%s" % (data['nick'], '', data['text'])
if cmd == 'onlineSet':
users = ''
for u in data['nicks']:
users = users + u + ',' + m_settings.username
users = users + u + ','
users += m_settings.username
if api == 0:
values = (data['ver'], data['cookie'])
return '欢迎来到十字街!请保证您已经阅读并同意了服务协议。\n当前聊天室版本: %s \n您的cookie:%s \n如果您所在的聊天室没有在线的用户,' \
Expand All @@ -100,13 +97,13 @@ def parse_cmds(data):

class MyClient(WebSocketClient):
def opened(self):
def update():
def update():
try:
update_api = 'https://api.github.com/repos/MuRongPIG/CrossStreet-python/releases/latest'
js = json.loads(requests.get(update_api).text)
tag_name = js.get('tag_name')
print(tag_name)
if tag_name != 'v1.2.2':
if tag_name != 'v1.5.1':
var_append(var_text,'有新版本可用:' + tag_name + '\n' + '请前往https://github.com/MuRongPIG/CrossStreet-python/releases/latest 查看')
else:
var_append(var_text,'CrossStreet-Python 已是最新版本')
Expand All @@ -132,14 +129,20 @@ def update():

def closed(self, code, reason=None):
global connected
print("Closed down:", code, reason)
#var_append("Closed down:"+code+reason)
connected = False

def send_text(self, text):
data = json.dumps({'cmd': 'chat', "text": text})
print('<-', data)
data = json.dumps({"cmd": "chat", "text": text})
print('<-', data,type(data))
self.send(data)

def send_all(self, text):
json_str = ast.literal_eval(text)
json_str_ = json.dumps(json_str)
print('<-', json_str_)
self.send(json_str_)

def received_message(self, resp):
global var_text
print('->', resp)
Expand All @@ -150,14 +153,14 @@ def received_message(self, resp):
return
data = resp
result = parse_cmds(data) + '\n'
result = emoji.demojize(result)
result = demojize(result)
# print(result)
var_append(var_text, result)


class ChatClientSetup:
def __init__(self, root=None):
global api
global api
api = 0
from tkinter import ttk
self.root = root
Expand All @@ -168,7 +171,7 @@ def __init__(self, root=None):
if platform.system() == 'Windows':
top.attributes("-toolwindow", 1)
top.attributes("-topmost", 1)
top.resizable(width=False, height=False)
top.resizable(width=True, height=True)
top.title("登录-新版十字街 By @MuRongPIG")
# top.overrideredirect(True)
# self.root.iconify()
Expand All @@ -182,30 +185,28 @@ def __init__(self, root=None):

frame = Frame(top)



self.setup_user.set(str(self.settings.username))
self.setup_password.set(str(self.settings.password))
self.setup_channel.set(str(self.settings.channel))

Label(frame, text='用户名').grid(row=1, column=1)
Entry(frame, textvariable=self.setup_user).grid(row=1, column=2)
Label(frame, text='密码').grid(row=2, column=1)
Entry(frame, textvariable=self.setup_password).grid(row=2, column=2)
Entry(frame, textvariable=self.setup_password,show='*').grid(row=2, column=2)
Label(frame, text='房间(默认%s)' % channel_).grid(row=3, column=1)
Entry(frame, textvariable=self.setup_channel).grid(row=3, column=2)


def go(*args):
global api
if comboxlist.get() == "crosst.chat":
api = 0
elif comboxlist.get() == "hack.chat":
if comboxlist.get() == "hack.chat":
api = 1

comboxlist=ttk.Combobox(frame) #初始化
comboxlist["values"]=("crosst.chat","hack.chat")
comboxlist.current(0) #选择第一个
comboxlist=ttk.Combobox(frame) #初始化
comboxlist["values"]=("crosst.chat","hack.chat")
comboxlist.current(0) #选择第一个
comboxlist.grid(row=4,column=2)
comboxlist.bind("<<ComboboxSelected>>",go)

Expand All @@ -223,9 +224,22 @@ def setup_confirm(self):
self.settings.save()
self.top.destroy()

client = ChatClient()
client.loop()


def loop(self):
self.root.mainloop()



def var_append(var, data):
my_text.configure(state='normal')
my_text.mark_set('insert', END)
# var.set(var.get() + '\n' + data)
my_text.insert(INSERT, '\n' + data)
my_text.mark_set('insert', END)
my_text.see(END)
my_text.configure(state='disabled')


class ChatClient:
Expand All @@ -243,12 +257,12 @@ def __init__(self, root=None):
self.root = root
if self.root is None:
self.root = Tk()
self.title = '聊天室 - %s' % self.username
self.title = '聊天室 - %s' % self.channel
self.root.title(self.title)

self.var_send = StringVar()
self.entry = Entry(self.root, textvariable=self.var_send)
self.entry.grid(columnspan=5, row=1, sticky=W + E)
self.entry.grid(column=0, columnspan=5, row=1, sticky=N + E +W)
self.entry.bind('<Return>', self.send_message)
global var_text
var_text = StringVar()
Expand All @@ -258,27 +272,43 @@ def __init__(self, root=None):
my_text = Text(self.root)
self.scroll.config(command=my_text.yview)
my_text.config(yscrollcommand=self.scroll.set)
my_text.grid(column=1, row=0)
self.scroll.grid(column=0, row=0, sticky=N + S)
my_text.grid(column=0, row=0)
self.scroll.grid(column=5, row=0, sticky=N + S + W)

self.button = Button(self.root, text="SEND", command=self.send_message)
self.button.grid(column=5, row=1)

self.frame = Frame(self.root)


self.var = IntVar()
self.var.set(1)

self.Radiobutton1 = Radiobutton(self.frame, text="发送信息/命令(默认)", value=1, variable=self.var)
self.Radiobutton1.grid(column=0, row=0, sticky=W)

self.Radiobutton1 = Radiobutton(self.frame, text="发送websocket信息", value=2, variable=self.var)
self.Radiobutton1.grid(column=1, row=0, sticky=W)
self.frame.grid(column=0, row=2)


self.ws = None
self.init_ws()
try:
update()
except:
pass

def init_ws(self):
self.ws = MyClient(self.api)
self.ws.connect()

def send_message(self, event=None):
type_ = self.var.get()
message = self.var_send.get()
self.ws.send_text(message)
self.var_send.set("")
if type_ == 1:
self.ws.send_text(message)
self.var_send.set("")
if type_ == 2:
self.ws.send_all(message)
self.var_send.set("")


def loop(self):
self.root.mainloop()
Expand All @@ -288,6 +318,3 @@ def loop(self):
_settings = Settings()
_setup = ChatClientSetup()
_setup.loop()
client = ChatClient()
client.loop()

0 comments on commit b0a4e08

Please sign in to comment.