mirror of
https://github.com/tengge1/ShadowEditor.git
synced 2026-01-25 15:08:11 +00:00
连接服务端WebSocket。
This commit is contained in:
parent
99cdfe9a3c
commit
46c8060fcd
@ -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
|
||||
};
|
||||
|
||||
// 获取用户登录信息
|
||||
|
||||
@ -13,7 +13,29 @@ namespace ShadowEditor.Server.Helpers
|
||||
public sealed class ConfigHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否开启权限
|
||||
/// Mongo数据库连接
|
||||
/// </summary>
|
||||
public static string MongoConnection
|
||||
{
|
||||
get
|
||||
{
|
||||
return ConfigurationManager.AppSettings["mongo_connection"];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Mongo数据库名称
|
||||
/// </summary>
|
||||
public static string MongoDBName
|
||||
{
|
||||
get
|
||||
{
|
||||
return ConfigurationManager.AppSettings["mongo_dbName"];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否开启权限管理,true: 开启, false: 关闭
|
||||
/// </summary>
|
||||
public static bool EnableAuthority
|
||||
{
|
||||
@ -24,7 +46,7 @@ namespace ShadowEditor.Server.Helpers
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 默认登录时长设置
|
||||
/// 登录时长设置,分钟,只允许整数。
|
||||
/// </summary>
|
||||
public static int Expires
|
||||
{
|
||||
@ -33,5 +55,38 @@ namespace ShadowEditor.Server.Helpers
|
||||
return Convert.ToInt32(ConfigurationManager.AppSettings["Expires"].ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否开启远程编辑,true: 开启,false: 关闭
|
||||
/// </summary>
|
||||
public static bool EnableRemoteEdit
|
||||
{
|
||||
get
|
||||
{
|
||||
return ConfigurationManager.AppSettings["EnableRemoteEdit"] == "true";
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// FPT服务器端口(远程编辑用)
|
||||
/// </summary>
|
||||
public static int FTPServerPort
|
||||
{
|
||||
get
|
||||
{
|
||||
return Convert.ToInt32(ConfigurationManager.AppSettings["FTPServerPort"]);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// WebSocket服务器端口(远程编辑用)
|
||||
/// </summary>
|
||||
public static int WebSocketServerPort
|
||||
{
|
||||
get
|
||||
{
|
||||
return Convert.ToInt32(ConfigurationManager.AppSettings["WebSocketServerPort"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,12 +22,7 @@ namespace ShadowEditor.Server.Remote
|
||||
/// </summary>
|
||||
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<SocketServer>("/Socket");
|
||||
webSocketServer = new WebSocketServer(null, ConfigHelper.WebSocketServerPort);
|
||||
webSocketServer.AddWebSocketService<SocketServer>("/RemoteEdit");
|
||||
webSocketServer.Start();
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
@ -9,13 +9,15 @@
|
||||
</configSections>
|
||||
<!-- 应用程序配置 -->
|
||||
<appSettings>
|
||||
<!-- Mongo数据库连接 -->
|
||||
<add key="mongo_connection" value="mongodb://127.0.0.1:27017" />
|
||||
<!-- Mongo数据库名称 -->
|
||||
<add key="mongo_dbName" value="ShadowEditor" />
|
||||
<!-- 是否开启权限管理,true: 开启, false: 关闭 -->
|
||||
<add key="EnableAuthority" value="false" />
|
||||
<!-- 登录时长设置,分钟,只允许整数。 -->
|
||||
<add key="Expires" value="120" />
|
||||
<!-- 是否开发远程编辑,true: 开启,false: 关闭 -->
|
||||
<!-- 是否开启远程编辑,true: 开启,false: 关闭 -->
|
||||
<add key="EnableRemoteEdit" value="true" />
|
||||
<!-- FPT服务器端口(远程编辑用) -->
|
||||
<add key="FTPServerPort" value="2121" />
|
||||
|
||||
@ -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(),
|
||||
|
||||
65
ShadowEditor.Web/src/event/WebSocketEvent.js
Normal file
65
ShadowEditor.Web/src/event/WebSocketEvent.js
Normal file
@ -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;
|
||||
@ -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);
|
||||
|
||||
@ -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;
|
||||
Loading…
x
Reference in New Issue
Block a user