Yuki Takei 5 лет назад
Родитель
Сommit
8f306d91bc

+ 6 - 9
src/client/js/services/WebsocketContainer.js

@@ -1,6 +1,6 @@
 import { Container } from 'unstated';
 
-import ReconnectingWebSocket from 'reconnecting-websocket';
+import io from 'socket.io-client';
 
 /**
  * Service container related to options for WebSocket
@@ -14,17 +14,14 @@ export default class WebsocketContainer extends Container {
     this.appContainer = appContainer;
     this.appContainer.registerContainer(this);
 
+    this.socket = io({
+      transports: ['websocket'],
+    });
+    this.socketClientId = Math.floor(Math.random() * 100000);
+
     this.state = {
     };
 
-    this.socket = null;
-
-    this.initSocket();
-  }
-
-  initSocket() {
-    this.socket = new ReconnectingWebSocket();
-    this.socketClientId = Math.floor(Math.random() * 100000);
   }
 
   /**

+ 10 - 11
src/server/crowi/index.js

@@ -53,6 +53,7 @@ function Crowi(rootdir) {
   this.exportService = null;
   this.importService = null;
   this.searchService = null;
+  this.socketIoService = null;
   this.cdnResourcesService = new CdnResourcesService();
   this.interceptorManager = new InterceptorManager();
   this.xss = new Xss();
@@ -270,11 +271,6 @@ Crowi.prototype.setupModels = async function() {
   });
 };
 
-// FIXME: with GW-3262
-// Crowi.prototype.getIo = function() {
-//   return this.io;
-// };
-
 Crowi.prototype.scanRuntimeVersions = async function() {
   const self = this;
 
@@ -400,12 +396,8 @@ Crowi.prototype.start = async function() {
     }
   });
 
-  // setup WebSocket
-  // FIXME: with GW-3262
-  // const io = require('socket.io')(serverListening);
-  // io.sockets.on('connection', (socket) => {
-  // });
-  // this.io = io;
+  // setup socket.io
+  await this.setupSocketIoService(serverListening);
 
   // setup Express Routes
   this.setupRoutesAtLast();
@@ -583,4 +575,11 @@ Crowi.prototype.setupImport = async function() {
   }
 };
 
+Crowi.prototype.setupSocketIoService = async function(server) {
+  const SocketIoService = require('../service/socket-io');
+  if (this.socketIoService == null) {
+    this.socketIoService = new SocketIoService(server);
+  }
+};
+
 module.exports = Crowi;

+ 17 - 0
src/server/service/socket-io.js

@@ -0,0 +1,17 @@
+const socketIo = require('socket.io');
+
+class SocketIoService {
+
+  constructor(server) {
+    this.io = socketIo(server, {
+      transports: ['websocket'],
+    });
+  }
+
+  getIo() {
+    return this.io;
+  }
+
+}
+
+module.exports = SocketIoService;