| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
'use strict'; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
ve.dm.CollabTransportServer = function VeDmCollabTransportServer( startHeight ) { |
| 16 |
const startTimestamp = Date.now(); |
| 17 |
this.startHeight = startHeight; |
| 18 |
this.protocolServer = new ve.dm.ProtocolServer( |
| 19 |
{ |
| 20 |
startHeight: startHeight, |
| 21 |
|
| 22 |
serverId: 've-collab-server', |
| 23 |
load: function () { |
| 24 |
return Promise.resolve( |
| 25 |
ve.dm.Change.static.deserialize( { transactions: [] } ) |
| 26 |
); |
| 27 |
}, |
| 28 |
onNewChange: function () { |
| 29 |
return Promise.resolve(); |
| 30 |
} |
| 31 |
}, |
| 32 |
{ |
| 33 |
|
| 34 |
logServerEvent: ( x ) => console.log( x ), |
| 35 |
|
| 36 |
logEvent: ( x ) => console.log( x ), |
| 37 |
getRelativeTimestamp: () => Date.now() - startTimestamp |
| 38 |
} |
| 39 |
); |
| 40 |
this.connections = []; |
| 41 |
}; |
| 42 |
|
| 43 |
OO.initClass( ve.dm.CollabTransportServer ); |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
ve.dm.CollabTransportServer.prototype.onConnection = function ( conn ) { |
| 51 |
const context = this.protocolServer.authenticate( 've-collab-doc', null, null ), |
| 52 |
connections = this.connections, |
| 53 |
server = this.protocolServer, |
| 54 |
startHeight = this.startHeight; |
| 55 |
|
| 56 |
connections.push( conn ); |
| 57 |
|
| 58 |
context.broadcast = function ( type, data ) { |
| 59 |
const serialized = ve.collab.serialize( data ); |
| 60 |
connections.forEach( ( connection ) => { |
| 61 |
connection.send( { type: type, data: serialized } ); |
| 62 |
} ); |
| 63 |
}; |
| 64 |
context.sendAuthor = function ( type, data ) { |
| 65 |
const serialized = ve.collab.serialize( data ); |
| 66 |
conn.send( { type: type, data: serialized } ); |
| 67 |
}; |
| 68 |
conn.on( 'data', ( data ) => { |
| 69 |
const type = data.type; |
| 70 |
if ( type === 'submitChange' ) { |
| 71 |
server.onSubmitChange( context, data.data ); |
| 72 |
} else if ( type === 'changeAuthor' ) { |
| 73 |
server.onChangeAuthor( context, data.data ); |
| 74 |
} else if ( type === 'disconnect' ) { |
| 75 |
server.onDisconnect( context, data.data ); |
| 76 |
} else if ( type === 'logEvent' ) { |
| 77 |
|
| 78 |
} else { |
| 79 |
throw new Error( 'Unknown type "' + type + '"' ); |
| 80 |
} |
| 81 |
} ); |
| 82 |
conn.on( 'open', () => { |
| 83 |
server.welcomeClient( |
| 84 |
context, |
| 85 |
startHeight, |
| 86 |
( authorId ) => ve.msg( 'visualeditor-collab-user-placeholder', authorId ) |
| 87 |
); |
| 88 |
} ); |
| 89 |
}; |