diff --git a/ShadowEditor.Server/Controllers/System/ConfigController.cs b/ShadowEditor.Server/Controllers/System/ConfigController.cs index e3fcb232..d40fcf7c 100644 --- a/ShadowEditor.Server/Controllers/System/ConfigController.cs +++ b/ShadowEditor.Server/Controllers/System/ConfigController.cs @@ -63,7 +63,9 @@ namespace ShadowEditor.Server.Controllers.System ["RoleName"] = "", ["DeptID"] = 0, ["DeptName"] = "", - ["OperatingAuthorities"] = new JArray() + ["OperatingAuthorities"] = new JArray(), + ["EnableRemoteEdit"] = ConfigHelper.EnableRemoteEdit, + ["WebSocketServerPort"] = ConfigHelper.WebSocketServerPort }; // 获取用户登录信息 diff --git a/ShadowEditor.Server/Helpers/ConfigHelper.cs b/ShadowEditor.Server/Helpers/ConfigHelper.cs index c9ccc343..82060c6d 100644 --- a/ShadowEditor.Server/Helpers/ConfigHelper.cs +++ b/ShadowEditor.Server/Helpers/ConfigHelper.cs @@ -13,7 +13,29 @@ namespace ShadowEditor.Server.Helpers public sealed class ConfigHelper { /// - /// 是否开启权限 + /// Mongo数据库连接 + /// + public static string MongoConnection + { + get + { + return ConfigurationManager.AppSettings["mongo_connection"]; + } + } + + /// + /// Mongo数据库名称 + /// + public static string MongoDBName + { + get + { + return ConfigurationManager.AppSettings["mongo_dbName"]; + } + } + + /// + /// 是否开启权限管理,true: 开启, false: 关闭 /// public static bool EnableAuthority { @@ -24,7 +46,7 @@ namespace ShadowEditor.Server.Helpers } /// - /// 默认登录时长设置 + /// 登录时长设置,分钟,只允许整数。 /// public static int Expires { @@ -33,5 +55,38 @@ namespace ShadowEditor.Server.Helpers return Convert.ToInt32(ConfigurationManager.AppSettings["Expires"].ToString()); } } + + /// + /// 是否开启远程编辑,true: 开启,false: 关闭 + /// + public static bool EnableRemoteEdit + { + get + { + return ConfigurationManager.AppSettings["EnableRemoteEdit"] == "true"; + } + } + + /// + /// FPT服务器端口(远程编辑用) + /// + public static int FTPServerPort + { + get + { + return Convert.ToInt32(ConfigurationManager.AppSettings["FTPServerPort"]); + } + } + + /// + /// WebSocket服务器端口(远程编辑用) + /// + public static int WebSocketServerPort + { + get + { + return Convert.ToInt32(ConfigurationManager.AppSettings["WebSocketServerPort"]); + } + } } } diff --git a/ShadowEditor.Server/Remote/RemoteServer.cs b/ShadowEditor.Server/Remote/RemoteServer.cs index 50d7f17a..32e3ead8 100644 --- a/ShadowEditor.Server/Remote/RemoteServer.cs +++ b/ShadowEditor.Server/Remote/RemoteServer.cs @@ -22,12 +22,7 @@ namespace ShadowEditor.Server.Remote /// public class RemoteServer { - // ftp - private string ftpPort = ConfigurationManager.AppSettings["FTPServerPort"]; private FtpServer ftpServer = null; - - // websocket - private string webSocketPort = ConfigurationManager.AppSettings["WebSocketServerPort"]; private WebSocketServer webSocketServer = null; public void Start() @@ -51,7 +46,7 @@ namespace ShadowEditor.Server.Remote fsProvider, membershipProvider, "127.0.0.1", - int.Parse(ftpPort), + ConfigHelper.FTPServerPort, new AssemblyFtpCommandHandlerFactory(typeof(FtpServer).GetTypeInfo().Assembly) ); ftpServer.Start(); @@ -65,8 +60,8 @@ namespace ShadowEditor.Server.Remote try { // see: https://github.com/jjrdk/websocket-sharp - webSocketServer = new WebSocketServer(null, int.Parse(webSocketPort)); - webSocketServer.AddWebSocketService("/Socket"); + webSocketServer = new WebSocketServer(null, ConfigHelper.WebSocketServerPort); + webSocketServer.AddWebSocketService("/RemoteEdit"); webSocketServer.Start(); } catch (Exception ex) diff --git a/ShadowEditor.Web/Web.config b/ShadowEditor.Web/Web.config index f7f05f50..ef3fb27b 100644 --- a/ShadowEditor.Web/Web.config +++ b/ShadowEditor.Web/Web.config @@ -9,13 +9,15 @@ + + - + diff --git a/ShadowEditor.Web/src/event/EventDispatcher.js b/ShadowEditor.Web/src/event/EventDispatcher.js index 8327da1e..efc4b77f 100644 --- a/ShadowEditor.Web/src/event/EventDispatcher.js +++ b/ShadowEditor.Web/src/event/EventDispatcher.js @@ -9,6 +9,7 @@ import ResizeEvent from './ResizeEvent'; import FilterEvent from './FilterEvent'; import ViewEvent from './ViewEvent'; import GPUPickEvent from './GPUPickEvent'; +import WebSocketEvent from './WebSocketEvent'; import TransformControlsEvent from './TransformControlsEvent'; import ObjectEvent from './ObjectEvent'; @@ -38,6 +39,7 @@ function EventDispatcher() { new FilterEvent(), new ViewEvent(), new GPUPickEvent(), + new WebSocketEvent(), // viewport中的事件 new TransformControlsEvent(), diff --git a/ShadowEditor.Web/src/event/WebSocketEvent.js b/ShadowEditor.Web/src/event/WebSocketEvent.js new file mode 100644 index 00000000..d1efc713 --- /dev/null +++ b/ShadowEditor.Web/src/event/WebSocketEvent.js @@ -0,0 +1,65 @@ +import BaseEvent from './BaseEvent'; + +/** + * WebSocket事件 + * @author tengge / https://github.com/tengge1 + */ +class WebSocketEvent extends BaseEvent { + constructor() { + super(); + + this.reconnectTime = 5000; // 重新连接时间 + + this.onOpen = this.onOpen.bind(this); + this.onMessage = this.onMessage.bind(this); + this.onError = this.onError.bind(this); + this.onClose = this.onClose.bind(this); + } + + start() { + if (!app.server.enableRemoteEdit) { + return; + } + + let url = `ws://${new URL(app.options.server).hostname}:${app.server.webSocketServerPort}/RemoteEdit`; + + try { + this.socket = new WebSocket(url); + } catch (e) { + console.warn(e); + } + + this.socket.addEventListener('open', this.onOpen); + this.socket.addEventListener('message', this.onMessage); + this.socket.addEventListener('error', this.onError); + this.socket.addEventListener('close', this.onClose); + } + + stop() { + if (!this.socket) { + return; + } + this.socket.removeEventListener('open', this.onOpen); + this.socket.removeEventListener('message', this.onMessage); + this.socket.removeEventListener('error', this.onError); + this.socket.removeEventListener('close', this.onClose); + } + + onOpen(event) { + console.log('WebSocket open successfully.'); + } + + onMessage(event) { + console.log('WebSocket message received.'); + } + + onError(event) { + console.warn('WebSocket Error:', event); + } + + onClose(event) { + console.log('WebSocket closed.'); + } +} + +export default WebSocketEvent; \ No newline at end of file diff --git a/ShadowEditor.Web/src/utils/Server.js b/ShadowEditor.Web/src/utils/Server.js index 8ae7fd14..9bf7c20e 100644 --- a/ShadowEditor.Web/src/utils/Server.js +++ b/ShadowEditor.Web/src/utils/Server.js @@ -16,6 +16,9 @@ class Server { this.authorities = []; // 权限列表 this.isAdmin = false; // 是否是管理员 + + this.enableRemoteEdit = false; + this.webSocketServerPort = 5000; } load() { @@ -38,6 +41,9 @@ class Server { this.authorities = obj.Data.OperatingAuthorities; this.isAdmin = this.roleName === 'Administrator'; + + this.enableRemoteEdit = obj.Data.EnableRemoteEdit; + this.webSocketServerPort = obj.Data.WebSocketServerPort; resolve(); }).catch(e => { console.warn(e); diff --git a/ShadowEditor.Web/src/utils/Socket.js b/ShadowEditor.Web/src/utils/Socket.js deleted file mode 100644 index f65e25c5..00000000 --- a/ShadowEditor.Web/src/utils/Socket.js +++ /dev/null @@ -1,43 +0,0 @@ -import { dispatch } from '../third_party'; - -/** - * Socket工具类 - * @author tengge / https://github.com/tengge1 - * @param {*} url Socket服务器地址 - */ -function Socket(url) { - this.url = url; - this.reconnectTime = 5000; // 重新连接时间 - - this.socket = new WebSocket(this.url); - - this.dispatch = dispatch('open', 'message', 'error', 'close'); - - var _this = this; - this.socket.onopen = function (event) { - _this.dispatch.call.apply(_this.dispatch, event); - }; - - this.socket.onmessage = function (event) { - _this.dispatch.call.apply(_this.dispatch, event); - }; - - this.socket.onerror = function (event) { - _this.dispatch.call.apply(_this.dispatch, event); - }; - - this.socket.onclose = function (event) { - _this.dispatch.call.apply(_this.dispatch, event); - if (this.reconnectTime !== null) { - setTimeout(function () { - _this.socket = new WebSocket(this.url); - }, this.reconnectTime); - } - }; -} - -Socket.prototype.on = function (eventName, callback) { - this.dispatch.on(eventName, callback); -}; - -export default Socket; \ No newline at end of file