2

Below is my code -

<apex:page docType="html-5.0" showHeader="false" sidebar="false">
<html>

    <head>
        <script type="text/javascript" src="http://cs8.salesforce.com/support/api/28.0/interaction.js"></script>
        <script type="text/javascript">
            var webSocket;
        var data = "";

        function openSocket()
        {
            // Open server socket
            if (webSocket !== undefined && webSocket.readyState !== WebSocket.CLOSED) {
                alert("WebSocket is already opened");
                return;
            }

            webSocket = new WebSocket('wss://13.65.193.191:8003/test');

            if (webSocket === undefined)
            {
                alert("Error creating socket...");
                return;
            }

            webSocket.onopen = function()
            {
                alert("in onopen callback");
            }

            webSocket.onmessage = function(event)
            {
                alert("in onmessage callback   " + data);
            }

            webSocket.onclose = function()
            {
                alert("in onclose callback");
            }
        }

        </script>   

        <script type="text/javascript">
        function WebSocketTest()
        {
            if ("WebSocket" in window)
            {
                alert("WebSocket is supported by your Browser!");

                // Let us open a web socket

                var ws = new WebSocket('wss://13.65.193.191:8003/test');
                ws.onopen = function()
                {
                    // Web Socket is connected, send data using send()
                    final_query = {query: 'hi',dialog: 'initial',intent: ''};
                    json_query = JSON.stringify(final_query);
                    ws.send(json_query);
                    alert("Message is sent... "+final_query);
                };

                ws.onmessage = function (evt) 
                { 
                    var received_msg = evt.data;
                    var json=JSON.parse(received_msg);
                    alert("Message is received... "+json);
                };

                ws.onclose = function()
                { 
                    // websocket is closed.
                    alert("Connection is closed..."); 
                };
            }

            else
            {
                // The browser doesn't support WebSocket
                alert("WebSocket NOT supported by your Browser!");
            }
        }
        </script>
    </head>
    <body>
        <button onclick="openSocket();">Open Socket</button>
        <div id="sse">
            <a href="javascript:WebSocketTest()">Run WebSocket</a>
        </div>
        <input placeholder="Your id" id='name' name="name" type="text" class="form-control" required="autofocus" />
    </body>
</html>

I am getting the below exception -

WebSocket connection to 'wss://xx.xx.xxx.191:8003/test' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED openSocket @ TestBot:23 onclick @ TestBot:91

TestBot:58 WebSocket connection to 'ws://xx.xx.xxx.191:8003/test' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

Does salesforce support websockets from vf pages?

2

Yes, you can use Websockets from a visualforce page.

Here is a nice example: Example Websockets from VF Page

3
  • usually best practice on SFSE is to include relevant bits into your answer and not just link to another 3rd party site as that link could go dead
    – cropredy
    Nov 24 '17 at 23:34
  • In Example they have use - var socket = io.connect("192.168.1.100", {port: 3001}) i need to connect using WebSocket. Nov 25 '17 at 4:15
  • do you have any example where they have use WebSocket ? Nov 25 '17 at 4:15
0

@Nirmallya Ghosh your doing correct, did you checked with socket that is created in WSS OR WS , and is that Socket is running in forever mode. check this using MQTT.Fx application to check whether your socket is running with same credeantials.

are able to solve this? if not try with Paho client libraries

https://www.eclipse.org/paho/clients/js/

you can connect a wedsocket from this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.