Skip to content

Commit 4b6bc90

Browse files
committed
U 优化web终端对中文编码支持
1 parent e77cad7 commit 4b6bc90

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

spug_api/consumer/consumers.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ def loop_read(self):
9393
if not data:
9494
self.close(3333)
9595
break
96-
self.send(bytes_data=data)
96+
try:
97+
text = data.decode()
98+
except UnicodeDecodeError:
99+
text = data.decode(encoding='GBK', errors='ignore')
100+
self.send(text_data=text)
97101

98102
def receive(self, text_data=None, bytes_data=None):
99103
data = text_data or bytes_data
@@ -120,15 +124,15 @@ def connect(self):
120124
self.close()
121125

122126
def _init(self):
123-
self.send(bytes_data=b'\r\33[KConnecting ...\r')
127+
self.send(text_data='\r\33[KConnecting ...\r')
124128
host = Host.objects.filter(pk=self.id).first()
125129
if not host:
126130
self.send(text_data='Unknown host\r\n')
127131
self.close()
128132
try:
129133
self.ssh = host.get_ssh().get_client()
130134
except Exception as e:
131-
self.send(bytes_data=f'Exception: {e}\r\n'.encode())
135+
self.send(text_data=f'Exception: {e}\r\n'.encode())
132136
self.close()
133137
return
134138
self.chan = self.ssh.invoke_shell(term='xterm')

spug_web/src/pages/ssh/Terminal.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function WebSSH(props) {
2424
term.write('WebSocket connecting ... ');
2525
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
2626
const socket = new WebSocket(`${protocol}//${window.location.host}/api/ws/ssh/${props.id}/?x-token=${X_TOKEN}`);
27-
socket.onmessage = e => _read_as_text(e.data);
27+
socket.onmessage = e => term.write(e.data)
2828
socket.onopen = () => {
2929
term.write('ok')
3030
term.focus();
@@ -68,12 +68,6 @@ function WebSSH(props) {
6868
}
6969
}
7070

71-
function _read_as_text(data) {
72-
const reader = new window.FileReader();
73-
reader.onload = () => term.write(reader.result);
74-
reader.readAsText(data, 'utf-8')
75-
}
76-
7771
return (
7872
<div className={styles.termContainer}>
7973
<div className={styles.terminal} ref={container}/>

0 commit comments

Comments
 (0)