6
6
import { User } from '@jupyterlab/services' ;
7
7
8
8
import { IDisposable } from '@lumino/disposable' ;
9
- import { ISignal , Signal } from '@lumino/signaling' ;
9
+ import { IStream , Stream } from '@lumino/signaling' ;
10
+
11
+ import { IAwareness } from '@jupyter/ydoc' ;
10
12
11
13
import * as decoding from 'lib0/decoding' ;
12
14
import * as encoding from 'lib0/encoding' ;
13
15
import { WebsocketProvider } from 'y-websocket' ;
14
16
15
- import { IAwareness , IAwarenessProvider } from './tokens' ;
17
+ import { IAwarenessProvider } from './tokens' ;
16
18
17
19
export enum MessageType {
18
20
CHAT = 125
19
21
}
20
22
23
+ export interface IContent {
24
+ type : string ;
25
+ body : string ;
26
+ }
27
+
21
28
export interface IChatMessage {
22
- username : string ;
23
- msg : string ;
29
+ sender : string ;
30
+ timestamp : number ;
31
+ content : IContent ;
24
32
}
25
33
26
34
/**
@@ -45,13 +53,13 @@ export class WebSocketAwarenessProvider
45
53
46
54
this . _awareness = options . awareness ;
47
55
48
- const user = options . user ;
49
- user . ready
50
- . then ( ( ) => this . _onUserChanged ( user ) )
56
+ this . _user = options . user ;
57
+ this . _user . ready
58
+ . then ( ( ) => this . _onUserChanged ( this . _user ) )
51
59
. catch ( e => console . error ( e ) ) ;
52
- user . userChanged . connect ( this . _onUserChanged , this ) ;
60
+ this . _user . userChanged . connect ( this . _onUserChanged , this ) ;
53
61
54
- this . _chatMessage = new Signal ( this ) ;
62
+ this . _messageStream = new Stream ( this ) ;
55
63
56
64
this . messageHandlers [ MessageType . CHAT ] = (
57
65
encoder ,
@@ -62,8 +70,7 @@ export class WebSocketAwarenessProvider
62
70
) => {
63
71
const content = decoding . readVarString ( decoder ) ;
64
72
const data = JSON . parse ( content ) as IChatMessage ;
65
- console . debug ( 'Chat:' , data ) ;
66
- this . _chatMessage . emit ( data ) ;
73
+ this . _messageStream . emit ( data ) ;
67
74
} ;
68
75
}
69
76
@@ -74,17 +81,18 @@ export class WebSocketAwarenessProvider
74
81
/**
75
82
* A signal to subscribe for incoming messages.
76
83
*/
77
- get chatMessage ( ) : ISignal < this, IChatMessage > {
78
- return this . _chatMessage ;
84
+ get messageStream ( ) : IStream < this, IChatMessage > {
85
+ return this . _messageStream ;
79
86
}
80
87
81
88
dispose ( ) : void {
82
89
if ( this . _isDisposed ) {
83
90
return ;
84
91
}
85
92
86
- this . destroy ( ) ;
93
+ this . _user . userChanged . disconnect ( this . _onUserChanged , this ) ;
87
94
this . _isDisposed = true ;
95
+ this . destroy ( ) ;
88
96
}
89
97
90
98
/**
@@ -93,10 +101,13 @@ export class WebSocketAwarenessProvider
93
101
* @param msg message
94
102
*/
95
103
sendMessage ( msg : string ) : void {
96
- console . debug ( 'Send message:' , msg ) ;
104
+ const data : IContent = {
105
+ type : 'text' ,
106
+ body : msg
107
+ } ;
97
108
const encoder = encoding . createEncoder ( ) ;
98
109
encoding . writeVarUint ( encoder , MessageType . CHAT ) ;
99
- encoding . writeVarString ( encoder , msg ) ;
110
+ encoding . writeVarString ( encoder , JSON . stringify ( data ) ) ;
100
111
this . ws ! . send ( encoding . toUint8Array ( encoder ) ) ;
101
112
}
102
113
@@ -105,9 +116,10 @@ export class WebSocketAwarenessProvider
105
116
}
106
117
107
118
private _isDisposed = false ;
119
+ private _user : User . IManager ;
108
120
private _awareness : IAwareness ;
109
121
110
- private _chatMessage : Signal < this, IChatMessage > ;
122
+ private _messageStream : Stream < this, IChatMessage > ;
111
123
}
112
124
113
125
/**
0 commit comments