Fork me on GitHub Fork me on GitHub

webSocket


<%
//Server
var log = new Log();
webSocket.ontext = function (data) {
    log.info('Client Sent : ' + data);
    var ws = this;
    setTimeout(function () {
       var currentDate = new Date();
       ws.send("Server Receive message "+data+" on "+currentDate);
    }, 1000);
};
 
webSocket.onbinary = function (stream) {
    log.info('Client Streamed : ' + stream.toString());
};
%>

//Client
<script>
var url = 'ws://localhost:9763/sample/ws/server.jag';
function start() {
ws = new WebSocket(url);
ws.onopen = function() {
	document.write("web Socket onopen. ");
};
//event handler for the message event in the case of text frames
ws.onmessage = function(event) {
	document.write("web Socket Onmessage from Server. " + event.data);
};
ws.onclose = function() {
	document.write("web Socket onclose. ");
};
ws.onopen();
var currentDate = new Date();
  //send message to server
  setTimeout(function() {
	 ws.send("Client Timestamp "+currentDate);
  }, 2000);
 
  //close socket from client
  setTimeout(function() {
	  ws.close();
  }, 5000);
 
}
</script>

Overview

Jaggery websocket enables full duplex transmission and it supports WebSocket specification. It defines an API establishing "socket" connections between a web browser and a server. In Simple words, There is an persistent connection between the client and the server and both parties can start sending data at any time. (WSO2 ELB do not support webSocket)


Members

Member Type Description
ontextScriptable
webSocket.ontext = function (data) { 
	log.info('Client sent : ' + data);
};

Create the instance that will process this inbound connection for text and allow to handle a GET request.

onbinaryScriptable
webSocket.onopen = function (stream) { 
	log.info('Client Open : ' + stream.toString());
};

Create the instance that will process when socket connection is established.

onopenScriptable
webSocket.onbinary = function (stream) { 
	log.info('Client Streamed : ' + stream.toString()); 
};

Create the instance that will process this inbound connection for stream.

oncloseScriptable
webSocket.onclose = function (stream) { 
	log.info('Client Close')
};

Create the instance that will process when connection is closed.

Operations

Operation Returns Description
send (String text | Stream stream)None

Optional HTTP headers.

{ 
	"Content-Type": "application/json", 
	"User-Agent": "Mozilla/5.0", 
	"Cookie": "JSESSIONID=4DA02FE19BA086964227"
}