Commit 79e360c7 by zhanghaozhe

ml小程序

parent f4d7f1c7
......@@ -5,6 +5,7 @@ import 'xterm/css/xterm.css'
import { FitAddon } from "xterm-addon-fit";
import { http } from "@/utils"
import { Toast } from "antd-mobile";
import axios from 'axios'
class TerminalInterface extends Component {
......@@ -44,15 +45,15 @@ class TerminalInterface extends Component {
}
getAuthenticationData = () => {
http.post(`http://47.93.119.175:8888/auth`, {
axios.post(`http://47.93.119.175:8888/auth`, {
username: '3guh394h3fhj0f4',
password: 'okqdw029j038hrv3890cv',
}, {
withCredentials: false
withCredentials: false,
}).then(res => {
const {code, msg, data} = res.data
if (code === 200) {
this.getLinkId(data.token)
if (code === 0) {
this.getLinkId(data)
} else {
Toast.fail(msg, 2)
}
......@@ -60,41 +61,44 @@ class TerminalInterface extends Component {
}
getLinkId = token => {
http.post(`http://47.93.119.175:8888`, {}, {
axios.post(`http://47.93.119.175:8888`, {}, {
headers: {
Token: token,
},
withCredentials: false,
}).then(res => {
const {code, msg, data} = res.data
if (code === 200) {
this.connectServer(data.id)
} else {
Toast.fail(msg, 2, null, false)
}
const {id} = res.data
this.connectServer(id)
})
}
connectServer = (id) => {
this.socket = new WebSocket(`${API.ws}?id=${id}`)
this.socket.addEventListener('open', () => {
console.log('opened')
this.socket.send(JSON.stringify({
data: this.props.filename,
}))
})
this.socket.onmessage = event => {
console.log(event.message)
this.terminal.write(event.data)
const reader = new FileReader()
reader.onload = () => {
this.terminal.write(this.ab2str(reader.result))
}
reader.readAsArrayBuffer(event.data)
}
this.socket.onerror = (event) => {
console.log(event)
// this.connectServer(id)
}
this.socket.onclose = (event) => {
console.log('closed')
console.log(event)
}
}
ab2str = buf => {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}
render() {
const {terminalWidth} = this.state
return (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment