Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Chrome TCP Backend
import 'text-encoding';
import uuid from 'uuid';
// I am not really sure about this master luke.
const ChromeSocketServer = chrome.sockets.tcpServer;
const ChromeSocketClient = chrome.sockets.tcp;
const serverSocketListeners = {};
const clientSocketListeners = {};
const textEncoder = new TextEncoder();
// maybe use some weakmap foo here ?
function queueReceiveListener(socketId, callback){
clientSocketListeners[socketId] = callback;
}
function queueConnectedListener(socketId, callback){
serverSocketListeners[socketId] = callback;
}
function onAcceptListener(info){
const {socketId, clientSocketId} = info;
$log("Accepted", info);
serverSocketListeners[socketId] && serverSocketListeners[socketId]({
socketId: clientSocketId,
serverSocketId: socketId,
isOpen: true,
});
}
function onAcceptErrorListener(info){
$log("Accepted Error", info);
const {socketId, clientSocketId} = info;
}
function onRecieveListener(info){
const {socketId, data} = info;
$log("Recieved", info);
clientSocketListeners[socketId] && clientSocketListeners[socketId](data);
}
function onRecieveErrorListener(info){
$log("Recieve Error", info);
}
ChromeSocketServer.onAccept.addListener(onAcceptListener);
ChromeSocketClient.onReceive.addListener(onRecieveListener);
ChromeSocketServer.onAcceptError.addListener(onAcceptErrorListener);
ChromeSocketClient.onReceiveError.addListener(onRecieveErrorListener);
export default {
stringToBuffer(str){
$log('Encoding to buffer', str);
return textEncoder.encode(str);
},
stringToSocketData(str){
$log('Encoding to data', str);
return textEncoder.encode(str);
},
createServerHandle(httpServer){
$log('creating server handle', httpServer);
const handle = {
socketId: -1,
server: httpServer
};
ChromeSocketServer.create({}, createInfo => {
const {socketId} = createInfo;
handle.socketId = socketId;
queueConnectedListener(socketId, (connSocket) => {
$log('Creating conenction socket', connSocket);
queueReceiveListener(connSocket.socketId, (data) => {
$log('Recieved Data', data);
httpServer._dataHandler(connSocket, data);
});
httpServer._connectionHandler(connSocket);
});
});
return handle;
},
listen(handle, port){
const {socketId} = handle;
$log('Listening on', handle, port);
if( socketId == -1 ){
// maybe queue this ?
throw new Error('Socket, not ready yet');
}
ChromeSocketServer.listen(socketId, '192.168.1.5', port, (err) => {
if( err !== 0 ) throw new Error('Failed to connect, reason ' + err);
});
},
unlisten(handle){
const {socketId} = handle;
$log('Un Listening on', handle);
ChromeSocketServer.disconnect(socketId, () =>{
handle.isClosed = true;
});
},
sendAndClose(socket, arrayBuffer){
const {socketId} = socket;
$log('Closing the pipe and close', socket, arrayBuffer);
ChromeSocketClient.send(socketId, arrayBuffer, () => {
ChromeSocketClient.close(socketId);
});
},
// I think this applies only to a client socket.
// not sure though.
close(socket){
const {socketId} = socket;
$log('Closing the pipe', socket);
ChromeSocketClient.close(socketId);
},
send(socket, arrayBuffer){
const {socketId} = socket;
$log('Writing to pipe', socket, arrayBuffer);
ChromeSocketClient.send(socketId, arrayBuffer);
},
// dunno what this is for.
connect(handle, ip, port){
throw new Error('Unimplemented');
},
// This should be done soon.
// should be more or less same as the server
// socket guy, maybe we should abstract client and
// server sockets into classes ?
createClientHandle(){
throw new Error('Unimplemented');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.