Connect websocket client to server

pull/262/head
ioedeveloper 4 years ago committed by yann300
parent 949e6211f0
commit add05a54af
  1. 14
      src/app.js
  2. 107
      src/app/files/remixDProvider.js
  3. 6
      src/app/files/remixd-handle.js
  4. 1
      src/lib/remixd.js

@ -122,6 +122,7 @@ var css = csjs`
class App { class App {
constructor (api = {}, events = {}, opts = {}) { constructor (api = {}, events = {}, opts = {}) {
var self = this var self = this
self.appManager = new RemixAppManager({})
self._components = {} self._components = {}
self._view = {} self._view = {}
self._view.splashScreen = yo` self._view.splashScreen = yo`
@ -142,18 +143,17 @@ class App {
registry.put({api: config, name: 'config'}) registry.put({api: config, name: 'config'})
// load file system // load file system
self.appManager = new RemixAppManager({})
self._components.filesProviders = {} self._components.filesProviders = {}
self._components.filesProviders['browser'] = new FileProvider('browser') self._components.filesProviders['browser'] = new FileProvider('browser')
registry.put({api: self._components.filesProviders['browser'], name: 'fileproviders/browser'}) registry.put({api: self._components.filesProviders['browser'], name: 'fileproviders/browser'})
// var remixd = new Remixd(65520) var remixd = new Remixd(65520)
// registry.put({api: remixd, name: 'remixd'}) registry.put({api: remixd, name: 'remixd'})
// remixd.event.register('system', (message) => { remixd.event.register('system', (message) => {
// if (message.error) toolTip(message.error) if (message.error) toolTip(message.error)
// }) })
self._components.filesProviders['localhost'] = new RemixDProvider(self.appManager) self._components.filesProviders['localhost'] = new RemixDProvider(remixd, self.appManager)
registry.put({api: self._components.filesProviders['localhost'], name: 'fileproviders/localhost'}) registry.put({api: self._components.filesProviders['localhost'], name: 'fileproviders/localhost'})
registry.put({api: self._components.filesProviders, name: 'fileproviders'}) registry.put({api: self._components.filesProviders, name: 'fileproviders'})

@ -3,10 +3,11 @@ var EventManager = require('../../lib/events')
var pathtool = require('path') var pathtool = require('path')
module.exports = class RemixDProvider { module.exports = class RemixDProvider {
constructor (appManager) { constructor (remixd, appManager) {
this._appManager = appManager
this.remixd = remixapi(appManager, this)
this.event = new EventManager() this.event = new EventManager()
this._remixd = remixd
this._appManager = appManager
this.remixd = remixapi(remixd, this)
this.type = 'localhost' this.type = 'localhost'
this.error = { 'EEXIST': 'File already exists' } this.error = { 'EEXIST': 'File already exists' }
this._isReady = false this._isReady = false
@ -17,37 +18,37 @@ module.exports = class RemixDProvider {
var remixdEvents = ['connecting', 'connected', 'errored', 'closed'] var remixdEvents = ['connecting', 'connected', 'errored', 'closed']
remixdEvents.forEach((value) => { remixdEvents.forEach((value) => {
// remixd.event.register(value, (event) => { remixd.event.register(value, (event) => {
// this.event.trigger(value, [event]) this.event.trigger(value, [event])
// }) })
}) })
// remixd.event.register('notified', (data) => { remixd.event.register('notified', (data) => {
// if (data.scope === 'sharedfolder') { if (data.scope === 'sharedfolder') {
// if (data.name === 'created') { if (data.name === 'created') {
// this.init(() => { this.init(() => {
// this.event.trigger('fileAdded', [this.type + '/' + data.value.path, data.value.isReadOnly, data.value.isFolder]) this.event.trigger('fileAdded', [this.type + '/' + data.value.path, data.value.isReadOnly, data.value.isFolder])
// }) })
// } else if (data.name === 'removed') { } else if (data.name === 'removed') {
// this.init(() => { this.init(() => {
// this.event.trigger('fileRemoved', [this.type + '/' + data.value.path]) this.event.trigger('fileRemoved', [this.type + '/' + data.value.path])
// }) })
// } else if (data.name === 'changed') { } else if (data.name === 'changed') {
// this._appManager.call('sharedfolder', 'get', {path: data.value}, (error, content) => { this._appManager.call('remixd', 'get', {path: data.value}, (error, content) => {
// if (error) { if (error) {
// console.log(error) console.log(error)
// } else { } else {
// var path = this.type + '/' + data.value var path = this.type + '/' + data.value
// this.filesContent[path] = content this.filesContent[path] = content
// this.event.trigger('fileExternallyChanged', [path, content]) this.event.trigger('fileExternallyChanged', [path, content])
// } }
// }) })
// } else if (data.name === 'rootFolderChanged') { } else if (data.name === 'rootFolderChanged') {
// // new path has been set, we should reset // new path has been set, we should reset
// this.event.trigger('folderAdded', [this.type + '/']) this.event.trigger('folderAdded', [this.type + '/'])
// } }
// } }
// }) })
} }
isConnected () { isConnected () {
@ -55,17 +56,18 @@ module.exports = class RemixDProvider {
} }
close (cb) { close (cb) {
this.remixd.exit()
this._isReady = false this._isReady = false
cb() cb()
} }
async init (cb) { async init (cb) {
const result = await this._appManager.call('remixdWebsocketPlugin', 'folderIsReadOnly', {}) this._isReady = true
this._readOnlyMode = result this._readOnlyMode = await this._appManager.call('remixd', 'folderIsReadOnly', {})
console.log('this._readOnlyMode: ', this._readOnlyMode)
cb()
} }
// @TODO: refactor all `this._appManager.call(....)` uses into `this.remixd[api](...)` // @TODO: refactor all `this._remixd.call(....)` uses into `this.remixd[api](...)`
// where `api = ...`: // where `api = ...`:
// this.remixd.read(path, (error, content) => {}) // this.remixd.read(path, (error, content) => {})
// this.remixd.write(path, content, (error, result) => {}) // this.remixd.write(path, content, (error, result) => {})
@ -77,7 +79,7 @@ module.exports = class RemixDProvider {
async exists (path, cb) { async exists (path, cb) {
const unprefixedpath = this.removePrefix(path) const unprefixedpath = this.removePrefix(path)
const callId = await this._appManager.call('sharedfolder', 'exists', {path: unprefixedpath}) const callId = await this._appManager.call('remixd', 'exists', {path: unprefixedpath})
const result = await this._appManager.receiveResponse(callId) const result = await this._appManager.receiveResponse(callId)
return cb(null, result) return cb(null, result)
@ -93,7 +95,7 @@ module.exports = class RemixDProvider {
get (path, cb) { get (path, cb) {
var unprefixedpath = this.removePrefix(path) var unprefixedpath = this.removePrefix(path)
this._appManager.call('sharedfolder', 'get', {path: unprefixedpath}, (error, file) => { this._appManager.call('remixd', 'get', {path: unprefixedpath}, (error, file) => {
if (!error) { if (!error) {
this.filesContent[path] = file.content this.filesContent[path] = file.content
if (file.readonly) { this._readOnlyFiles[path] = 1 } if (file.readonly) { this._readOnlyFiles[path] = 1 }
@ -108,7 +110,7 @@ module.exports = class RemixDProvider {
set (path, content, cb) { set (path, content, cb) {
var unprefixedpath = this.removePrefix(path) var unprefixedpath = this.removePrefix(path)
this._appManager.call('sharedfolder', 'set', {path: unprefixedpath, content: content}, (error, result) => { this._appManager.call('remixd', 'set', {path: unprefixedpath, content: content}, (error, result) => {
if (cb) return cb(error, result) if (cb) return cb(error, result)
var path = this.type + '/' + unprefixedpath var path = this.type + '/' + unprefixedpath
this.event.trigger('fileChanged', [path]) this.event.trigger('fileChanged', [path])
@ -122,7 +124,7 @@ module.exports = class RemixDProvider {
async remove (path) { async remove (path) {
var unprefixedpath = this.removePrefix(path) var unprefixedpath = this.removePrefix(path)
const callId = await this._appManager.call('sharedfolder', 'remove', {path: unprefixedpath}, (error, result) => { const callId = await this._appManager.call('remixd', 'remove', {path: unprefixedpath}, (error, result) => {
if (error) console.log(error) if (error) console.log(error)
var path = this.type + '/' + unprefixedpath var path = this.type + '/' + unprefixedpath
delete this.filesContent[path] delete this.filesContent[path]
@ -137,7 +139,7 @@ module.exports = class RemixDProvider {
rename (oldPath, newPath, isFolder) { rename (oldPath, newPath, isFolder) {
var unprefixedoldPath = this.removePrefix(oldPath) var unprefixedoldPath = this.removePrefix(oldPath)
var unprefixednewPath = this.removePrefix(newPath) var unprefixednewPath = this.removePrefix(newPath)
this._appManager.call('sharedfolder', 'rename', {oldPath: unprefixedoldPath, newPath: unprefixednewPath}, (error, result) => { this._appManager.call('remixd', 'rename', {oldPath: unprefixedoldPath, newPath: unprefixednewPath}, (error, result) => {
if (error) { if (error) {
console.log(error) console.log(error)
if (this.error[error.code]) error = this.error[error.code] if (this.error[error.code]) error = this.error[error.code]
@ -175,47 +177,46 @@ module.exports = class RemixDProvider {
async isDirectory (path) { async isDirectory (path) {
const unprefixedpath = this.removePrefix(path) const unprefixedpath = this.removePrefix(path)
const callId = await this._appManager.call('sharedfolder', 'isDirectory', {path: unprefixedpath}) const callId = await this._appManager.call('remixd', 'isDirectory', {path: unprefixedpath})
return await this._appManager.receiveResponse(callId) return await this._remixd.receiveResponse(callId)
} }
async isFile (path) { async isFile (path) {
const unprefixedpath = this.removePrefix(path) const unprefixedpath = this.removePrefix(path)
const callId = await this._appManager.call('sharedfolder', 'isFile', {path: unprefixedpath}) const callId = await this._appManager.call('remixd', 'isFile', {path: unprefixedpath})
return await this._appManager.receiveResponse(callId) return await this._remixd.receiveResponse(callId)
} }
} }
function remixapi (appManager, self) { function remixapi (remixd, self) {
const read = (path, callback) => { const read = (path, callback) => {
path = '' + (path || '') path = '' + (path || '')
path = pathtool.join('./', path) path = pathtool.join('./', path)
appManager.call('sharedfolder', 'get', { path }, (error, content) => callback(error, content)) remixd.call('remixd', 'get', { path }, (error, content) => callback(error, content))
} }
const write = (path, content, callback) => { const write = (path, content, callback) => {
path = '' + (path || '') path = '' + (path || '')
path = pathtool.join('./', path) path = pathtool.join('./', path)
appManager.call('sharedfolder', 'set', { path, content }, (error, result) => callback(error, result)) remixd.call('remixd', 'set', { path, content }, (error, result) => callback(error, result))
} }
const rename = (path, newpath, callback) => { const rename = (path, newpath, callback) => {
path = '' + (path || '') path = '' + (path || '')
path = pathtool.join('./', path) path = pathtool.join('./', path)
appManager.call('sharedfolder', 'rename', { oldPath: path, newPath: newpath }, (error, result) => callback(error, result)) remixd.call('remixd', 'rename', { oldPath: path, newPath: newpath }, (error, result) => callback(error, result))
} }
const remove = (path, callback) => { const remove = (path, callback) => {
path = '' + (path || '') path = '' + (path || '')
path = pathtool.join('./', path) path = pathtool.join('./', path)
appManager.call('sharedfolder', 'remove', { path }, (error, result) => callback(error, result)) remixd.call('remixd', 'remove', { path }, (error, result) => callback(error, result))
} }
const dir = (path, callback) => { const dir = (path, callback) => {
path = '' + (path || '') path = '' + (path || '')
path = pathtool.join('./', path) path = pathtool.join('./', path)
appManager.call('sharedfolder', 'resolveDirectory', { path }, (error, filesList) => callback(error, filesList)) remixd.call('remixd', 'resolveDirectory', { path }, (error, filesList) => callback(error, filesList))
} }
const exit = () => { remixd.close() } const exit = () => { remixd.close() }
// const api = { read, write, rename, remove, dir, exit, event: remixd.event } const api = { read, write, rename, remove, dir, exit, event: remixd.event }
const api = { read, write, rename, remove, dir, exit, event: self.event }
return api return api
} }

@ -20,7 +20,9 @@ var css = csjs`
const profile = { const profile = {
name: 'remixd', name: 'remixd',
url: 'ws://127.0.0.1:65520',
methods: ['folderIsReadOnly'], methods: ['folderIsReadOnly'],
events: [],
description: 'Using Remixd daemon, allow to access file system', description: 'Using Remixd daemon, allow to access file system',
kind: 'other', kind: 'other',
version: packageJson.version version: packageJson.version
@ -35,12 +37,14 @@ export class RemixdHandle extends WebsocketPlugin {
} }
deactivate () { deactivate () {
super.deactivate()
this.locahostProvider.close((error) => { this.locahostProvider.close((error) => {
if (error) console.log(error) if (error) console.log(error)
}) })
} }
activate () { async activate () {
await super.activate()
this.connectToLocalhost() this.connectToLocalhost()
} }

@ -106,6 +106,7 @@ class Remixd {
reject(error) reject(error)
return return
} }
console.log('this.socket: ', this.socket)
if (this.socket && this.socket.readyState === this.socket.OPEN) { if (this.socket && this.socket.readyState === this.socket.OPEN) {
var data = this.format(service, fn, args) var data = this.format(service, fn, args)
this.callbacks[data.id] = callback this.callbacks[data.id] = callback

Loading…
Cancel
Save