Skip to content

Instantly share code, notes, and snippets.

@mimura1133
Created January 5, 2022 05:54
Show Gist options
  • Save mimura1133/bfdc02df989ccc3d063b28e024a2c359 to your computer and use it in GitHub Desktop.
Save mimura1133/bfdc02df989ccc3d063b28e024a2c359 to your computer and use it in GitHub Desktop.
/*
HTTPS サーバを用意して、Header に下記のパラメータを設定する必要あり。
Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Opener-Policy: same-origin
Permissions-Policy: direct-sockets=(self)
---
99.0.4807.0 現在、起動時は下記パラメータの設定も必要
chrome --enable-blink-features=DirectSockets --enable-features=DirectSockets --restricted-api-origins=https://(用意したサイトのアドレス)
*/
const options = {
remoteAddress: '(IP ADDR)',
remotePort: (PORT)
};
navigator.openTCPSocket(options).then(tcpSocket => {
window.writer = tcpSocket.writable.getWriter();
window.reader = tcpSocket.readable.getReader();
})
function str2ab(str) {
var buf = new ArrayBuffer(str.length);
var bufView = new Uint8Array(buf);
for (var i=0, strLen=str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}
function send(str){
writer.ready.then(() => {
writer.write(str2ab(str+"\n"));
reader.read().then( ({done, value}) => {
console.log(ab2str(value));
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment