Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
H
Hubs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ar-noc
Hubs
Commits
3e87d0d4
Commit
3e87d0d4
authored
6 years ago
by
Greg Fodor
Browse files
Options
Downloads
Patches
Plain Diff
xfer working
parent
3340d10c
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/hub.js
+0
-3
0 additions, 3 deletions
src/hub.js
src/utils/xfer-channel.js
+44
-3
44 additions, 3 deletions
src/utils/xfer-channel.js
with
44 additions
and
6 deletions
src/hub.js
+
0
−
3
View file @
3e87d0d4
...
...
@@ -384,9 +384,6 @@ const onReady = async () => {
});
xferChannel
.
setSocket
(
socket
);
const
code
=
await
xferChannel
.
generateCode
();
code
.
onFinished
.
then
(
reason
=>
console
.
log
(
reason
));
console
.
log
(
code
);
};
document
.
addEventListener
(
"
DOMContentLoaded
"
,
onReady
);
This diff is collapsed.
Click to expand it.
src/utils/xfer-channel.js
+
44
−
3
View file @
3e87d0d4
...
...
@@ -13,8 +13,9 @@ export default class XferChannel {
//
// cancel: A function that the caller can call to cancel the use of the code.
//
// onFinished: A promise that, when resolved, indicates the code was used or expired.
// If expired, the string "expired" will be passed forward.
// onFinished: A promise that, when resolved, indicates the code is no longer usable,
// because it was either successfully used by the remote device or it has expired
// ("used" or "expired" is passed to the callback.)
generateCode
=
()
=>
{
return
new
Promise
(
resolve
=>
{
const
onFinished
=
new
Promise
(
finished
=>
{
...
...
@@ -57,11 +58,51 @@ export default class XferChannel {
}
});
channel
.
join
();
channel
.
join
()
.
receive
(
"
error
"
,
r
=>
console
.
error
(
r
))
;
};
step
();
});
});
};
// Attempts to receive an xfer payload from a remote device using the given code.
//
// Promise rejects if the code is invalid or there is a problem with the channel.
// Promise resolves and passes payload of xfer source on successful xfer.
attemptXfer
=
code
=>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
const
channel
=
this
.
socket
.
channel
(
`xfer:
${
code
}
`
,
{
timeout
:
10000
});
let
finished
=
false
;
channel
.
on
(
"
presence_state
"
,
state
=>
{
const
numOccupants
=
Object
.
keys
(
state
).
length
;
if
(
numOccupants
===
1
)
{
// Great, only sender is in topic, request xfer
channel
.
push
(
"
xfer_request
"
,
{});
setTimeout
(()
=>
{
if
(
finished
)
return
;
channel
.
leave
();
reject
(
"
no_response
"
);
},
10000
);
}
else
if
(
numOccupants
===
0
)
{
// Nobody in this channel, probably a bad code.
reject
(
"
failed
"
);
}
else
{
console
.
warn
(
"
xfer code channel already has 2 or more occupants, something fishy is going on.
"
);
reject
(
"
in_use
"
);
}
});
channel
.
on
(
"
xfer_response
"
,
payload
=>
{
finished
=
true
;
channel
.
leave
();
resolve
(
payload
);
});
channel
.
join
().
receive
(
"
error
"
,
r
=>
console
.
error
(
r
));
});
};
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment