Hi Everyone.
I’m hoping someone can help as it seems such a simple thing to try to do.
I have the example “how to create a websocket server” set up and running from here:
https://www.wowza.com/docs/how-to-create-a-websocket-server
public class HTTPProviderWebSocket extends HTTPProvider2Base
{
private static final Class<HTTPProviderWebSocket> CLASS = HTTPProviderWebSocket.class;
private static final String CLASSNAME = "HTTPProviderWebSocket";
public static final String DATEFORMAT = "EEE, dd MMM yyyy HH:mm:ss";
public static final int TIMER_INTERVAL = 6000;
private FastDateFormat fastDateFormat = FastDateFormat.getInstance(DATEFORMAT, SystemUtils.gmtTimeZone, Locale.US);
private Timer timer = null;
private Object lock = new Object();
// WebSocket listener
class MyWebSocketListener extends WebSocketEventNotifyBase
{
@Override
public void onCreate(IWebSocketSession webSocketSession)
{
//WMSLoggerFactory.getLogger(CLASS).info(CLASSNAME+"#MyWebSocketListener.onCreate["+webSocketSession.getSessionId()+"]");
}
@Override
public void onDestroy(IWebSocketSession webSocketSession)
{
//WMSLoggerFactory.getLogger(CLASS).info(CLASSNAME+"#MyWebSocketListener.onDestroy["+webSocketSession.getSessionId()+"]");
}
@Override
public void onMessage(IWebSocketSession webSocketSession, WebSocketMessage message)
{
// echo messages we receive back to the browser
if (message.isText())
{
WebSocketMessage messageText = WebSocketMessage.createMessageText(webSocketSession.isMaskOutgoingMessages(), message.getValueString());
webSocketSession.sendMessage(messageText);
// WMSLoggerFactory.getLogger(CLASS).info(CLASSNAME+"#MyWebSocketListener.onMessage["+webSocketSession.getSessionId()+"][text]: "+message.getValueString());
}
else if (message.isBinary())
{
WebSocketMessage messageBinary = WebSocketMessage.createMessageBinary(webSocketSession.isMaskOutgoingMessages(), message.getBuffer(), message.getOffset(), message.getLen());
webSocketSession.sendMessage(messageBinary);
// WMSLoggerFactory.getLogger(CLASS).info(CLASSNAME+"#MyWebSocketListener.onMessage["+webSocketSession.getSessionId()+"][binary]: #"+BufferUtils.encodeHexString(message.getBuffer(), message.getOffset(), message.getLen()));
}
}
}
class MyTimerTask extends TimerTask
{
private IVHost vhost = null;
private WebSocketContext webSocketContext = null;
MyTimerTask(IVHost vhost)
{
this.vhost = vhost;
this.webSocketContext = vhost.getWebSocketContext();
}
@Override
public void run()
{
String messageStr = Server.getInstance().getVersion()+" date:"+fastDateFormat.format(new Date())+" GMT";
// broadcast message to all active sessions attached to this HTTPProvider
WebSocketMessage messageText = WebSocketMessage.createMessageText(webSocketContext.isMaskOutgoingMessages(), messageStr);
broadcastWebSocketMessage(messageText);
//WMSLoggerFactory.getLogger(CLASS).info(CLASSNAME+"#MyTimerTask.run: "+messageStr);
}
}
public void onHTTPRequest(IVHost vhost, IHTTPRequest req, IHTTPResponse resp)
{
if (!doHTTPAuthentication(vhost, req, resp))
return;
synchronized(lock)
{
// create timer task on first connection
if (this.timer == null)
{
this.timer = new Timer();
timer.scheduleAtFixedRate(new MyTimerTask(vhost), TIMER_INTERVAL, TIMER_INTERVAL);
}
}
// is this an upgrade request
if (req.isUpgradeRequest())
{
// it this an websocket upgrade request
String upgradeType = req.getHeader("upgrade");
if (upgradeType != null && upgradeType.equalsIgnoreCase(IWebSocketSession.HTTPHEADER_NAME))
{
// set response header to accept the upgrade
resp.setHeader("Upgrade", IWebSocketSession.HTTPHEADER_NAME);
resp.setHeader("Connection", "Upgrade");
// set the security hash
String webSocketKey = req.getHeader(IWebSocketSession.HTTPHEADER_SECKEY);
if (webSocketKey != null)
{
String digestStr = WebSocketUtils.createSecResponse(webSocketKey);
if (digestStr != null)
resp.setHeader(IWebSocketSession.HTTPHEADER_SECACCEPT, digestStr);
}
// set 101 response code to accept upgrade request
resp.setResponseCode(101);
// insert WebSocket listener for this session
resp.setUpgradeRequestProtocol(IHTTPResponse.UPGRADE_PROTOCOL_WEBSOCKETS, new MyWebSocketListener());
}
else
resp.setResponseCode(404); // return 404 if not websocket upgrade request
}
else
resp.setResponseCode(404); //return 404 if not upgrade request
}
}
And I can see it has a section to broadcast a message with the server version running on a timer and calling:
WebSocketMessage messageText = WebSocketMessage. *createMessageText* (webSocketContext.isMaskOutgoingMessages(), messageStr);
broadcastWebSocketMessage(messageText);
And I can also see it has an example of some code that will respond to an incoming message with an outgoing message with the same text.
What I’m very much failing to understand how to do - is to use this websocket server to send useful messages from within other custom modules.
So for example I have a ModuleListenWebRTCSession class that contains an onAppStart function and an onRTPSessionCreate function
public class ModuleListenWebRTCSession extends ModuleBase {
private static String[] securityPlayIPBlackListArray;
public void onAppStart(IApplicationInstance appInstance) {
String securityPlayIPBlackListString = appInstance.getProperties().getPropertyStr("securityPlayIPBlackList","");
if (!securityPlayIPBlackListString.isEmpty()) {
securityPlayIPBlackListArray = securityPlayIPBlackListString.trim().split("\\s*,\\s*");
}
else {
securityPlayIPBlackListArray = null;
}
}
public void onRTPSessionCreate(RTPSession rtpSession) {
if (rtpSession.isWebRTC() && rtpSession.getRTSPStream().getModeName() == "PLAY") {
Map<String, Object> userData = (Map<String, Object>)rtpSession.getWebRTCSession().getCommandRequest().getJSONEntries().get("userData");
String ip = rtpSession.getIp();
List<String> securityPlayIPBlackList = Arrays.asList(securityPlayIPBlackListArray);
if(securityPlayIPBlackList.contains(ip)){
getLogger().info("IP blocked: "+ip);
rtpSession.rejectSession();
}
}
}
}
All I want to do is send “Hello World” to the clients connected to the websocket HTTP provider from within either of the onAppStart or onRTPSessionCreate functions as an example.
If anyone has any suggestions I would really appreciate it.
Thanks in advance
Pip