フィルターのクリア

Can I refer callbacks from a socket class to the parent app in AppDesigner?

1 回表示 (過去 30 日間)
Avirut Mehta
Avirut Mehta 2020 年 3 月 9 日
回答済み: Nihal 2024 年 6 月 18 日
Right now I have an app that is meant to recieve external data from a socket and use it within the app. The thing is, I can't implement the entire socket interface myself within the app; instead I am using a WebSocket class library. I have a subclass implementing the socket class that looks something like this:
classdef myClient < WebSocketClient
methods
function obj = myClient(savepath, varargin)
%Constructor
obj@WebSocketClient(varargin{:});
end
end
methods (Access = protected)
function onOpen(obj,message)
fprintf('%s\n',message);
end
function onTextMessage(obj,message)
% from here, I want to send the recieved message as well as this object to my app
end
end
end
I also have a MATLAB app with an instantiation of myClient. What I want is for the MATLAB app to be notified/have one of its own functions called when the onTextMessage callback is triggered. How would this be possible?

回答 (1 件)

Nihal
Nihal 2024 年 6 月 18 日
  1. Define an Event in your myClient Class: First, add an event declaration inside your myClient class. This event represents the action of receiving a message.
  2. Trigger the Event on Message Reception: Modify the onTextMessage method in your myClient class so that whenever a message is received, this event is triggered and any additional information, like the received message, is passed along with the event.
  3. Listen for the Event in Your MATLAB App: In your MATLAB app, set up an event listener that listens for the event you defined. When setting up this listener, specify a method of your app to be called whenever the event is triggered.
  4. Handle the Event in Your App: Implement the method in your app that you specified as the event listener. This method will execute whenever the event is triggered, allowing you to handle the received message within your app.
By following these steps, your MATLAB app will be able to react to messages received by the myClient instance without directly managing the socket communication itself.

カテゴリ

Help Center および File ExchangeDevelop Apps Using App Designer についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by