Skip to content

Commit

Permalink
Fix WebREPL
Browse files Browse the repository at this point in the history
  • Loading branch information
vshymanskyy committed Jun 10, 2024
1 parent 8a6bc0a commit 40a6475
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
43 changes: 31 additions & 12 deletions ViperIDE.html
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,10 @@ <h1>ViperIDE</h1>
this.receivedData = ''
}

async requestAccess() {
throw new Error("Method 'requestAccess()' must be implemented.")
}

async connect() {
throw new Error("Method 'connect()' must be implemented.")
}
Expand Down Expand Up @@ -586,8 +590,11 @@ <h1>ViperIDE</h1>
}
}

async connect() {
async requestAccess() {
this.port = await this.serial.requestPort()
}

async connect() {
await this.port.open({ baudRate: 115200 })

this.reader = this.port.readable.getReader()
Expand Down Expand Up @@ -644,7 +651,7 @@ <h1>ViperIDE</h1>
}
}

async connect() {
async requestAccess() {
this.device = await navigator.bluetooth.requestDevice({
filters: [{
//services: [NUS_SERVICE],
Expand All @@ -653,6 +660,9 @@ <h1>ViperIDE</h1>
//acceptAllDevices: true,
optionalServices: [NUS_SERVICE],
})
}

async connect() {
this.server = await this.device.gatt.connect()
this.service = await this.server.getPrimaryService(NUS_SERVICE)
this.rx = await this.service.getCharacteristic(NUS_RX)
Expand Down Expand Up @@ -697,16 +707,18 @@ <h1>ViperIDE</h1>
this.socket = null
}

_connect(url) {
return new Promise(function(resolve, reject) {
const ws = new WebSocket(url)
ws.onopen = function() { resolve(ws) }
ws.onerror = function(err) { reject(err) }
})
async requestAccess() {
}

async connect() {
this.socket = await this._connect(this.url)
function _conn(url) {
return new Promise(function(resolve, reject) {
const ws = new WebSocket(url)
ws.onopen = function() { resolve(ws) }
ws.onerror = function(err) { reject(err) }
})
}
this.socket = await _conn(this.url)
this.socket.binaryType = 'arraybuffer'
this.socket.onmessage = (event) => {
if (this.receiveCallback) {
Expand Down Expand Up @@ -793,7 +805,7 @@ <h1>ViperIDE</h1>
url = prompt('WebREPL device address:', '192.168.1.123:8266')
if (!url) return;

if (!url.contains("://")) { url = "ws://" + url }
if (!url.includes("://")) { url = "ws://" + url }

if (window.location.protocol === "https:") {
/* Navigate to device, which should automatically reload and ask for WebREPL password */
Expand All @@ -804,8 +816,9 @@ <h1>ViperIDE</h1>
url = webrepl_url
}
const pass = prompt('Password:')
if (!pass || pass.length < 4) {
toastr.error('Password is required')
if (pass == null) { return }
if (pass.length < 4) {
toastr.error('Password is too short')
return
}
await disconnect()
Expand Down Expand Up @@ -846,6 +859,12 @@ <h1>ViperIDE</h1>
return
}

try {
await port.requestAccess()
} catch {
return
}

try {
await port.connect()
} catch(error) {
Expand Down
2 changes: 1 addition & 1 deletion webrepl_content.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
}
window.webrepl_url = 'ws://' + url

fetch('ViperIDE.html')
fetch('index.html')
.then(rsp => rsp.text() )
.then(data => document.write(data))
})();

0 comments on commit 40a6475

Please sign in to comment.