Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
L
luna
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ops
luna
Commits
10a202c6
Commit
10a202c6
authored
Jul 24, 2019
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Update] 修改支持neffos ws 重连
parent
fd36ddf8
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
34 deletions
+46
-34
globals.ts
src/app/globals.ts
+5
-9
socket.ts
src/app/utils/socket.ts
+41
-25
No files found.
src/app/globals.ts
View file @
10a202c6
...
@@ -4,7 +4,7 @@ import * as io from 'socket.io-client';
...
@@ -4,7 +4,7 @@ import * as io from 'socket.io-client';
import
*
as
neffos
from
'neffos.js'
;
import
*
as
neffos
from
'neffos.js'
;
import
{
Terminal
}
from
'xterm'
;
import
{
Terminal
}
from
'xterm'
;
// const abc = io.connect('/ssh');
// const abc = io.connect('/ssh');
import
{
getWsSock
,
Socket
}
from
'./utils/socket'
;
import
{
Socket
}
from
'./utils/socket'
;
const
scheme
=
document
.
location
.
protocol
===
'https:'
?
'wss'
:
'ws'
;
const
scheme
=
document
.
location
.
protocol
===
'https:'
?
'wss'
:
'ws'
;
const
port
=
document
.
location
.
port
?
':'
+
document
.
location
.
port
:
''
;
const
port
=
document
.
location
.
port
?
':'
+
document
.
location
.
port
:
''
;
...
@@ -139,21 +139,17 @@ export let Browser: {
...
@@ -139,21 +139,17 @@ export let Browser: {
vendor
:
navigator
.
vendor
,
vendor
:
navigator
.
vendor
,
};
};
export
let
wsEvent
:
{
event
:
string
;
data
:
any
;
};
export
const
i18n
=
new
Map
();
export
const
i18n
=
new
Map
();
export
async
function
getWsSocket
()
{
export
async
function
getWsSocket
()
{
if
(
TermWS
)
{
if
(
TermWS
)
{
return
TermWS
;
return
TermWS
;
}
}
TermWS
=
await
getWsSock
(
wsURL
,
'ssh'
);
TermWS
=
new
Socket
(
wsURL
,
'ssh'
);
if
(
!
TermWS
)
{
const
nsConn
=
await
TermWS
.
connect
();
if
(
!
nsConn
)
{
console
.
log
(
'Try to using socket.io protocol'
);
console
.
log
(
'Try to using socket.io protocol'
);
TermWS
=
io
.
connect
(
'/ssh'
);
TermWS
=
io
.
connect
(
'/ssh'
,
{
reconnectionAttempts
:
10
}
);
}
}
DataStore
.
socket
=
TermWS
;
DataStore
.
socket
=
TermWS
;
return
TermWS
;
return
TermWS
;
...
...
src/app/utils/socket.ts
View file @
10a202c6
import
{
EventEmitter
}
from
'events/events'
;
import
{
EventEmitter
}
from
'events/events'
;
import
{
NSConn
,
marshal
}
from
'neffos.js'
;
import
{
Conn
,
NSConn
,
marshal
}
from
'neffos.js'
;
import
*
as
neffos
from
'neffos.js'
;
import
*
as
neffos
from
'neffos.js'
;
export
class
Socket
{
export
class
Socket
{
conn
:
NSConn
;
conn
:
Conn
;
nsConn
:
NSConn
;
emitter
:
EventEmitter
;
emitter
:
EventEmitter
;
url
:
string
;
namespace
:
string
;
constructor
(
conn
:
NSConn
,
emitter
:
EventEmitter
)
{
constructor
(
url
:
string
,
namespace
:
string
)
{
this
.
conn
=
conn
;
this
.
url
=
url
;
this
.
emitter
=
emitter
;
this
.
namespace
=
namespace
;
}
emit
(
type
:
string
,
obj
:
any
)
{
const
msg
=
marshal
(
obj
);
this
.
conn
.
emit
(
type
,
msg
);
}
on
(
type
:
string
,
fn
:
Function
,
opt_scope
?:
any
,
opt_oneshot
?:
boolean
)
{
this
.
emitter
.
on
(
type
,
fn
,
opt_scope
,
opt_oneshot
);
}
}
}
export
async
function
getWsSock
(
url
:
string
,
namespace
:
string
):
Promise
<
Socket
>
{
async
connect
()
{
const
emitter
=
new
EventEmitter
();
const
emitter
=
new
EventEmitter
();
this
.
emitter
=
emitter
;
const
events
=
{
const
events
=
{
};
};
let
interva
l
;
let
interval
=
nul
l
;
events
[
namespace
]
=
{
events
[
this
.
namespace
]
=
{
_OnNamespaceConnected
:
function
(
ns
,
msg
)
{
_OnNamespaceConnected
:
(
ns
,
msg
)
=>
{
emitter
.
emit
(
'connect'
,
ns
);
emitter
.
emit
(
'connect'
,
ns
);
if
(
ns
.
conn
.
wasReconnected
())
{
this
.
conn
=
ns
.
conn
;
this
.
nsConn
=
ns
;
console
.
log
(
'Ws was reconnected'
);
}
interval
=
setInterval
(()
=>
ns
.
emit
(
'ping'
,
''
),
10000
);
interval
=
setInterval
(()
=>
ns
.
emit
(
'ping'
,
''
),
10000
);
},
},
...
@@ -52,15 +50,33 @@ export async function getWsSock(url: string, namespace: string): Promise<Socket>
...
@@ -52,15 +50,33 @@ export async function getWsSock(url: string, namespace: string): Promise<Socket>
};
};
const
options
=
{
const
options
=
{
reconnect
:
5000
,
reconnect
:
5000
,
headers
:
{
'X-Namespace'
:
'ssh'
},
};
};
const
conn
=
<
neffos
.
Conn
>
await
neffos
.
dial
(
url
,
events
,
options
)
this
.
conn
=
<
Conn
>
await
neffos
.
dial
(
this
.
url
,
events
,
options
)
.
catch
(
err
=>
{
.
catch
(
err
=>
{
console
.
log
(
'connect to neffos ws error: '
,
err
);
return
null
;
return
null
;
});
});
if
(
!
conn
)
{
if
(
!
this
.
conn
)
{
return
null
;
return
null
;
}
}
const
nsConn
=
<
neffos
.
NSConn
>
await
conn
.
connect
(
namespace
);
this
.
nsConn
=
<
NSConn
>
await
this
.
conn
.
connect
(
this
.
namespace
)
const
sock
=
new
Socket
(
nsConn
,
emitter
);
.
catch
(
err
=>
{
return
sock
;
console
.
log
(
'connect to namespace error: '
,
err
);
return
null
;
});
return
this
.
nsConn
;
}
emit
(
type
:
string
,
obj
:
any
)
{
const
msg
=
marshal
(
obj
);
this
.
nsConn
.
emit
(
type
,
msg
);
}
on
(
type
:
string
,
fn
:
Function
,
opt_scope
?:
any
,
opt_oneshot
?:
boolean
)
{
this
.
emitter
.
on
(
type
,
fn
,
opt_scope
,
opt_oneshot
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment