Commit 79e360c7 by zhanghaozhe

ml小程序

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