Skip to content

Commit

Permalink
Merge branch 'Tencent:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnoChenFx authored Aug 30, 2024
2 parents 3c98f8a + e97e71d commit 0efb48b
Show file tree
Hide file tree
Showing 33 changed files with 2,155 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ inputs:
options:
- Release
- Debug
websocket:
type: choice
description: enable websocket
default: ''
options:
- ''
- '--websocket'
GITHUB_TOKEN:
required:

Expand All @@ -23,7 +30,7 @@ runs:
cd unity
npm i
cd native_src
ANDROID_NDK=~/android-ndk-r21b node ../cli make --platform android --backend ${{ inputs.backend }} --config ${{ inputs.config }}
ANDROID_NDK=~/android-ndk-r21b node ../cli make --platform android --backend ${{ inputs.backend }} --config ${{ inputs.config }} ${{ inputs.websocket }}
- name: Upload
uses: actions/upload-artifact@v3
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ inputs:
options:
- Release
- Debug
websocket:
type: choice
description: enable websocket
default: ''
options:
- ''
- '--websocket'
GITHUB_TOKEN:
required:

Expand All @@ -23,7 +30,7 @@ runs:
cd unity
npm i
cd native_src
node ../cli make --platform ios --backend ${{ inputs.backend }} --config ${{ inputs.config }}
node ../cli make --platform ios --backend ${{ inputs.backend }} --config ${{ inputs.config }} ${{ inputs.websocket }}
- name: Upload
uses: actions/upload-artifact@v3
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ inputs:
options:
- Release
- Debug
websocket:
type: choice
description: enable websocket
default: ''
options:
- ''
- '--websocket'
GITHUB_TOKEN:
required:

Expand All @@ -29,7 +36,7 @@ runs:
cd unity
npm i
cd native_src
node ../cli make --platform linux --arch x64 --backend ${{ inputs.backend }} --config ${{ inputs.config }}
node ../cli make --platform linux --arch x64 --backend ${{ inputs.backend }} --config ${{ inputs.config }} ${{ inputs.websocket }}
- name: Upload
uses: actions/upload-artifact@v3
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ inputs:
options:
- Release
- Debug
websocket:
type: choice
description: enable websocket
default: ''
options:
- ''
- '--websocket'
GITHUB_TOKEN:
required:

Expand All @@ -23,7 +30,7 @@ runs:
cd unity
npm i
cd native_src
OHOS_NDK_HOME=~/ohos-sdk/linux/native node ../cli make --platform ohos --backend ${{ inputs.backend }} --config ${{ inputs.config }}
OHOS_NDK_HOME=~/ohos-sdk/linux/native node ../cli make --platform ohos --backend ${{ inputs.backend }} --config ${{ inputs.config }} ${{ inputs.websocket }}
- name: Upload
uses: actions/upload-artifact@v3
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ inputs:
options:
- Release
- Debug
websocket:
type: choice
description: enable websocket
default: ''
options:
- ''
- '--websocket'
GITHUB_TOKEN:
required:

Expand All @@ -23,7 +30,7 @@ runs:
cd unity
npm i
cd native_src
node ../cli make --platform osx --backend ${{ inputs.backend }} --config ${{ inputs.config }}
node ../cli make --platform osx --backend ${{ inputs.backend }} --config ${{ inputs.config }} ${{ inputs.websocket }}
- name: Upload
uses: actions/upload-artifact@v3
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ inputs:
- auto
- x64
- ia32
websocket:
type: choice
description: enable websocket
default: ''
options:
- ''
- '--websocket'
GITHUB_TOKEN:
required:

Expand All @@ -31,7 +38,7 @@ runs:
cd unity
npm i
cd native_src
node ../cli make --platform win --backend ${{ inputs.backend }} --config ${{ inputs.config }} --arch ${{ inputs.arch }}
node ../cli make --platform win --backend ${{ inputs.backend }} --config ${{ inputs.config }} --arch ${{ inputs.arch }} ${{ inputs.websocket }}
- name: Upload
uses: actions/upload-artifact@v3
with:
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/unity_build_plugins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
backend: v8_10.6.194
websocket: '--websocket'
- name: BuildAndroidQuickJS
uses: ./.github/workflows/composites/unity-build-plugins/android/
with:
Expand Down Expand Up @@ -79,6 +80,7 @@ jobs:
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
backend: v8_10.6.194
websocket: '--websocket'
- name: BuildOHOSQuickJS
uses: ./.github/workflows/composites/unity-build-plugins/ohos/
with:
Expand All @@ -105,6 +107,7 @@ jobs:
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
backend: v8_10.6.194
websocket: '--websocket'
- name: BuildiOSQuickjs
uses: ./.github/workflows/composites/unity-build-plugins/ios/
with:
Expand Down Expand Up @@ -136,6 +139,7 @@ jobs:
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
backend: v8_10.6.194
websocket: '--websocket'
- name: BuildOSXNodejs
uses: ./.github/workflows/composites/unity-build-plugins/osx/
with:
Expand Down Expand Up @@ -167,6 +171,7 @@ jobs:
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
backend: v8_10.6.194
websocket: '--websocket'
- name: BuildWindowsQuickjs
uses: ./.github/workflows/composites/unity-build-plugins/windows/
with:
Expand Down Expand Up @@ -198,6 +203,7 @@ jobs:
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
backend: v8_10.6.194
websocket: '--websocket'
- name: BuildWindowsQuickjs
uses: ./.github/workflows/composites/unity-build-plugins/linux/
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function addModule(m) {

function getModuleBySID(id) {
return tmpModuleStorage[id];
}``
}

let buildinModule = Object.create(null);
function executeModule(fullPath, script, debugPath, sid) {
Expand Down
143 changes: 143 additions & 0 deletions unity/Assets/core/upm/Runtime/Resources/puerts/websocketpp.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Tencent is pleased to support the open source community by making Puerts available.
* Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
* Puerts is licensed under the BSD 3-Clause License, except for the third-party components listed in the file 'LICENSE' which may be subject to their corresponding license terms.
* This file is subject to the terms and conditions defined in file 'LICENSE', which is part of this source code package.
*/

var global = global || globalThis || (function () { return this; }());

const WebSocketPP = global.WebSocketPP;
//global.WebSocketPP = undefined;

class EventTarget {
constructor() {
this.listeners = {};
}

addEventListener(type, callback) {
if (!(type in this.listeners)) {
this.listeners[type] = [];
}
this.listeners[type].push(callback);
}

removeEventListener(type, callback) {
if (!(type in this.listeners)) {
return;
}
const stack = this.listeners[type];
for (let i = 0; i < stack.length; i++) {
if (stack[i] === callback) {
stack.splice(i, 1);
return;
}
}
}

dispatchEvent(ev) {
if (!(ev.type in this.listeners)) {
return true;
}
const stack = this.listeners[ev.type].slice();

for (let i = 0; i < stack.length; i++) {
stack[i].call(this, ev);
}
return !ev.defaultPrevented;
}
}

const readyStates = ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'];

const poll_ws_objects = [];

class WebSocket extends EventTarget {
constructor(url, protocols) {
super();
if (protocols) throw new Error('do not support protocols argument');
this._raw = new WebSocketPP(url);
this._url = url;
// !!do not raise exception in handles.
this._raw.setHandles(
()=> {
this._readyState = WebSocket.OPEN;
this._addPendingEvent({type:'open'});
},
(data) => {
this._addPendingEvent({type:'message', data:data, origin:this._url});
},
(code, reason) => {
this._cleanup();
this._addPendingEvent({type:'close', code:code, reason: reason});
},
() => {
this._addPendingEvent({type:'error'});
this._cleanup();
this._addPendingEvent({type:'close', code:1006, reason: ""});
});

this._readyState = WebSocket.CONNECTING;
this._tid = setInterval(() => this._poll(), 1);
this._pendingEvents = [];
}

get url() {
return this._url;
}

get readyState() {
return this._readyState;
}

send(data) {
if (this._readyState !== WebSocket.OPEN) {
throw new Error(`WebSocket is not open: readyState ${this._readyState} (${readyStates[this._readyState]})`);
}
this._raw.send(data);
}

_cleanup() {
this._readyState = WebSocket.CLOSING;
}

_addPendingEvent(ev) {
this._pendingEvents.push(ev);
}

_poll() {
if (this._pendingEvents.length === 0 && this._readyState != WebSocket.CLOSING) {
this._raw.poll();
}
const ev = this._pendingEvents.shift();
if (ev) this.dispatchEvent(ev);
if (this._pendingEvents.length === 0 && this._readyState == WebSocket.CLOSING) {
this._raw = undefined;
clearInterval(this._tid);
this._readyState = WebSocket.CLOSED;
}
}

close(code, data) {
try {
this._raw.close(code, data);
} catch(e) {}
this._cleanup();
}

}

for (let i = 0; i < readyStates.length; i++) {
Object.defineProperty(WebSocket, readyStates[i], {
enumerable: true,
value: i
});

Object.defineProperty(WebSocket.prototype, readyStates[i], {
enumerable: true,
value: i
});
}

global.WebSocket = WebSocket;

4 changes: 4 additions & 0 deletions unity/Assets/core/upm/Runtime/Src/Default/JsEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ public JsEnv(ILoader loader, int debugPort, BackendType backend, IntPtr external
{
ExecuteModule("puerts/nodepatch.mjs");
}

#if !UNITY_WEBGL
ExecuteModule("puerts/websocketpp.mjs");
#endif

#if UNITY_EDITOR
if (OnJsEnvCreate != null)
Expand Down
3 changes: 3 additions & 0 deletions unity/Assets/core/upm/Runtime/Src/IL2Cpp/JsEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ public JsEnv(ILoader loader, int debugPort = -1)
ExecuteModule("puerts/events.mjs");
ExecuteModule("puerts/timer.mjs");
ExecuteModule("puerts/promises.mjs");
#if !UNITY_WEBGL
ExecuteModule("puerts/websocketpp.mjs");
#endif

this.debugPort = debugPort;
if (loader is IBuiltinLoadedListener)
Expand Down
Loading

0 comments on commit 0efb48b

Please sign in to comment.