Skip to content
This repository has been archived by the owner on Dec 13, 2024. It is now read-only.

Prepare for single websocket connection #16

Merged
merged 2 commits into from
Jun 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/connection.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module.exports =
class Connection
constructor: (@nsync) ->

connect: (@url, @opts) ->
@socket = new AtomSocket('fs', @url)
connect: (@url, @socketKey) ->
@socket = new AtomSocket(@socketKey, @url)

@socket.on 'open', =>
@nsync.connected()
Expand All @@ -25,7 +25,8 @@ class Connection
@nsync.init()

send: (msg) ->
@socket.send(msg)
preparedMessage = JSON.stringify(file_sync: msg)
@socket.send(preparedMessage)

reset: ->
@socket.reset()
Expand Down
6 changes: 3 additions & 3 deletions src/nsync-fs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class Nsync
configure: ({@expansionState, @localRoot, connection}) ->
@setLocalPaths()

{websocket, url, opts} = connection
@connection.connect(url, opts, websocket)
{url, socketKey} = connection
@connection.connect(url, socketKey)

@emitter.emit('did-configure')

Expand Down Expand Up @@ -119,7 +119,7 @@ class Nsync
if @isConnected is false
@disconnectedSend(convertedMsg)

@connection.send(JSON.stringify(convertedMsg))
@connection.send(convertedMsg)

# ------------------
# File introspection
Expand Down
9 changes: 6 additions & 3 deletions src/onmessage.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ messageStrategies = {
}

module.exports = onmessage = (message, nsync) ->

try
{type, data} = JSON.parse(message)
console.log 'nsync:received', {type}
{file_sync} = JSON.parse(message)
catch error
return console.error 'nsync:received:parse', {error}

if not file_sync?
return

{type, data} = file_sync
console.log 'nsync:received', {type}
strategy = messageStrategies[type]

if not strategy?
Expand Down