Expose the id of the user that sent a socket via the API
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
## In development
|
## In development
|
||||||
|
### New features
|
||||||
|
- The `this` value of functions now contains the id of the user that triggered the function execution.
|
||||||
|
|
||||||
### Bugfixes
|
### Bugfixes
|
||||||
- When an invalid user id is specified socketlib will now throw the correct error message.
|
- When an invalid user id is specified socketlib will now throw the correct error message.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## 1.0.3
|
## 1.0.3
|
||||||
### Bugfixes
|
### Bugfixes
|
||||||
- `executeFor` functions will no longer fail with an exception if a function scheduled to be called by the local user throws.
|
- `executeFor` functions will no longer fail with an exception if a function scheduled to be called by the local user throws.
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ class SocketlibSocket {
|
|||||||
async executeAsGM(handler, ...args) {
|
async executeAsGM(handler, ...args) {
|
||||||
const [name, func] = this._resolveFunction(handler);
|
const [name, func] = this._resolveFunction(handler);
|
||||||
if (game.user.isGM) {
|
if (game.user.isGM) {
|
||||||
return func(...args);
|
return this._executeLocal(func, ...args);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (!game.users.find(user => user.isGM && user.active)) {
|
if (!game.users.find(user => user.isGM && user.active)) {
|
||||||
@@ -97,7 +97,7 @@ class SocketlibSocket {
|
|||||||
async executeAsUser(handler, userId, ...args) {
|
async executeAsUser(handler, userId, ...args) {
|
||||||
const [name, func] = this._resolveFunction(handler);
|
const [name, func] = this._resolveFunction(handler);
|
||||||
if (userId === game.userId)
|
if (userId === game.userId)
|
||||||
return func(...args);
|
return this._executeLocal(func, ...args);
|
||||||
const user = game.users.get(userId);
|
const user = game.users.get(userId);
|
||||||
if (!user)
|
if (!user)
|
||||||
throw new errors.SocketlibInvalidUserError(`No user with id '${userId}' exists.`);
|
throw new errors.SocketlibInvalidUserError(`No user with id '${userId}' exists.`);
|
||||||
@@ -111,7 +111,7 @@ class SocketlibSocket {
|
|||||||
this._sendCommand(name, args, RECIPIENT_TYPES.ALL_GMS);
|
this._sendCommand(name, args, RECIPIENT_TYPES.ALL_GMS);
|
||||||
if (game.user.isGM) {
|
if (game.user.isGM) {
|
||||||
try {
|
try {
|
||||||
func(...args);
|
this._executeLocal(func, ...args);
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
@@ -128,7 +128,7 @@ class SocketlibSocket {
|
|||||||
const [name, func] = this._resolveFunction(handler);
|
const [name, func] = this._resolveFunction(handler);
|
||||||
this._sendCommand(name, args, RECIPIENT_TYPES.EVERYONE);
|
this._sendCommand(name, args, RECIPIENT_TYPES.EVERYONE);
|
||||||
try {
|
try {
|
||||||
func(...args);
|
this._executeLocal(func, ...args);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
}
|
}
|
||||||
@@ -149,7 +149,7 @@ class SocketlibSocket {
|
|||||||
this._sendCommand(name, args, recipients);
|
this._sendCommand(name, args, recipients);
|
||||||
if (currentUserIndex >= 0) {
|
if (currentUserIndex >= 0) {
|
||||||
try {
|
try {
|
||||||
func(...args);
|
this._executeLocal(func, ...args);
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
@@ -185,6 +185,11 @@ class SocketlibSocket {
|
|||||||
game.socket.emit(this.socketName, message);
|
game.socket.emit(this.socketName, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_executeLocal(func, ...args) {
|
||||||
|
const socketdata = {userId: game.userId};
|
||||||
|
return func.call({socketdata}, ...args);
|
||||||
|
}
|
||||||
|
|
||||||
_resolveFunction(func) {
|
_resolveFunction(func) {
|
||||||
if (func instanceof Function) {
|
if (func instanceof Function) {
|
||||||
const entry = Array.from(this.functions.entries()).find(([key, val]) => val === func);
|
const entry = Array.from(this.functions.entries()).find(([key, val]) => val === func);
|
||||||
@@ -200,14 +205,14 @@ class SocketlibSocket {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_onSocketReceived(message) {
|
_onSocketReceived(message, senderId) {
|
||||||
if (message.type === MESSAGE_TYPES.COMMAND || message.type === MESSAGE_TYPES.REQUEST)
|
if (message.type === MESSAGE_TYPES.COMMAND || message.type === MESSAGE_TYPES.REQUEST)
|
||||||
this._handleRequest(message);
|
this._handleRequest(message, senderId);
|
||||||
else
|
else
|
||||||
this._handleResponse(message);
|
this._handleResponse(message, senderId);
|
||||||
}
|
}
|
||||||
|
|
||||||
async _handleRequest(message) {
|
async _handleRequest(message, senderId) {
|
||||||
const {handlerName, args, recipient, id, type} = message;
|
const {handlerName, args, recipient, id, type} = message;
|
||||||
// Check if we're the recipient of the received message. If not, return early.
|
// Check if we're the recipient of the received message. If not, return early.
|
||||||
if (recipient instanceof Array) {
|
if (recipient instanceof Array) {
|
||||||
@@ -241,13 +246,15 @@ class SocketlibSocket {
|
|||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
const socketdata = {userId: senderId};
|
||||||
|
const _this = {socketdata};
|
||||||
if (type === MESSAGE_TYPES.COMMAND) {
|
if (type === MESSAGE_TYPES.COMMAND) {
|
||||||
func(...args);
|
func.call(_this, ...args);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
let result;
|
let result;
|
||||||
try {
|
try {
|
||||||
result = await func(...args);
|
result = await func.call(_this, ...args);
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
console.error(`An exception occured while executing handler '${name}'.`);
|
console.error(`An exception occured while executing handler '${name}'.`);
|
||||||
@@ -258,11 +265,12 @@ class SocketlibSocket {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_handleResponse(message) {
|
_handleResponse(message, senderId) {
|
||||||
const {id, result, type} = message;
|
const {id, result, type} = message;
|
||||||
const request = this.pendingRequests.get(id);
|
const request = this.pendingRequests.get(id);
|
||||||
if (!request)
|
if (!request)
|
||||||
return;
|
return;
|
||||||
|
// TODO Verify if the response comes from the correct sender, discard otherwise
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case MESSAGE_TYPES.RESULT:
|
case MESSAGE_TYPES.RESULT:
|
||||||
request.resolve(result);
|
request.resolve(result);
|
||||||
|
|||||||
Reference in New Issue
Block a user