Class wwWebSocket

The wwWebSocket class provides basic helpers to Web Connection's Web Socket implementation.

This class can:

  • Create an instance of Web Socket Message object
  • Ability to Send a Web Socket Message

This class can be used in two ways:

  • As incoming Message object in wwProcess::OnWebSocket(loSocket)
  • Standalone for sending a message outside of Web Connection

OnWebSocket Usage

FUNCTION OnWebSocket
************************************************************************
*  OnWebSocket
****************************************
***  Function: Fired when a Web Socket request comes in.
***    Assume: loMsg.Message, loMsg.UserId, loMsg.GroupId
***      Pass: loMsg             - Incoming (loMsg.Message) from Socket
***            loSocketBroadcast - Use to broadcast message to others
***    Return: nothing
************************************************************************
LPARAMETERS loSocket as wwWebSocketMessage
LOCAL lcMarkdown, loSocketMsg, loMsg

*** This is the Socket Payload object
loMsg = loSocket.oMessage

DO CASE
CASE loMsg.action == "broadcastchatmessage"
	loSocket.AddRecipient(loMsg.GroupId, "group")
	
	*** Parse incoming message as Markdown
	lcMarkdown = Markdown(loMsg.message)
	lcMarkdown = ALLTRIM(lcMarkdown) && RTRIM(LTRIM(lcMarkdown,0,"<p>"),"</p>")

    *** Change incoming message with updated HTML
	loMsg.message = lcMarkdown
	
	*** Broadcast the message
	loSocket.BroadcastMessage(loMsg)
ENDCASE

ENDFUNC
*   OnWebSocket

Standalone Usage

DO wwWebSockets
loSocket = CREATEOBJECT("wwWebSockets")
loSocket.cBaseUrl = "http://localhost:5200/"
loMsg = loSocket.GetMessageObject()

loMsg.userId = "RickFox"
loMsg.Message = "Hello from FoxPro " + TIME()
loMsg.action = "broadcastchatmessage"  && send to everybody
loSocket.BroadcastMessage(loMsg)

Class Members

MemberDescription

AddRecipient

o.AddRecipient(lcUserOrGroup,lcType)

BroadcastMessage

o.BroadcastMessage(loMsg)

CreateMessageObject

o.CreateMessageObject(llDontAssign)

cBaseUrl

The base URL to the Web Site to send to. Needs to be set when sending outside of Web Connection. Inside of Web Connection this value is automatically set based on the incoming request of the Web site.

oMessage

See also:

Class wwWebSocket

© West Wind Technologies, 1996-2021 • Updated: 03/10/21
Comment or report problem with topic