» Fig Leaf Software Home

We've Got You Covered.

Tuesday, June 3, 2014

Simple barebones CF10/CF11 WebSocket Example

Here’s a bare-bones example of setting up an HTML5 WebSockets chat service using ColdFusion 10/11:
1
2
3
4
5
<!--- application.cfc --->
<cfcomponent>
  <cfset this.name = "Cf11Examples">
  <cfset this.wschannels = [{name="chat"}]>
</cfcomponent>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<!--- index.cfm --->
<cfwebsocket name="webSocketObj"
             onMessage="messageHandler"
             onError="errorHandler"
             onOpen="openHandler"
             onClose="closeHandler"
             subscribeTo="chat"/>
              
<doctype html>
<head>
    <title>WebSocket Example</title>
    <script type="text/javascript">
         
        messageHandler =  function(aEvent,aToken) {
          
          if (aEvent.data) {
           var txt=document.getElementById("msgArea");
           txt.innerHTML += aEvent.data  +"<br />";
          }
        }
         
        openHandler = function() {
            alert("Connection is open");
        }
         
        closeHandler= function() {
            alert("Connection Closed");
        }
         
        errorHandler = function() {
            alert("Doh!");
            console.log(arguments);
        }
         
        sendMessage = function() {
            var text = window.prompt("Enter some text","");
            if (text) {
                webSocketObj.publish("chat", text);
            }
        }
    </script>
</head>
<body>
    <div id="msgArea" />
    <input type="button" value="Send Message" onClick="sendMessage()">
</body>
</html>

No comments:

Post a Comment

About Us

Fig Leaf Software is an award-winning team of imaginative designers, innovative developers, experienced instructors, and insightful strategists.

For over 20 years, we’ve helped a diverse range of clients...

Read More

Contact Us

202-797-7711

Fig Leaf Software

1400 16th Street NW
Suite 450
Washington, DC 20036

info@figleaf.com