jsxmlsocket

Project site
code.google.com
Description
A javascript interface to Adobe Flash's XMLSocket class
Last commit
11 years ago
License
MIT License
Last release
11 years ago
Repository
http://jsxmlsocket.googlecode.com/svn/trunk/

Summary

Introduction

jsXMLSocket allows you to do asynchronous communication with a server using Adobe Flash's XMLSocket class. The XMLSocket class allows you to send and receive null terminated messages. Contrary to what the name indicates jsXMLSocket can be used for sending any type of data as long as the messages are null terminated.

jsXMLSocket is based off jSocket which implements a more general access to sockets in javascript using Flash's Socket class. In many implementations jSocket can be unnecessarily complicated and slower than jsXMLSocket.

Documentation

jsXMLSocket works in an object oriented fashion around the jsXMLSocket object. Before loading jsxmlsocket.js you must have loaded jQuery.

The jsXMLSocket Object

Constructor

jsXMLSocket()

Contructor of the jXMLSocket Object.

Methods

jsXMLSocket.setup(target)

Call this method to setup the socket. jsXMLSocket.setup takes the required argument target, which should contain a jQuery Selector that points to a container where jSocket will place the flash file in. After the DOM has updated and the flash file has loaded, it will fire jsXMLSocket.onReady.

jsXMLSocket.connect(host, port)

Call this method to connect to a listening socket. Method takes two required arguments, host which should be a host name or a valid ip address to connect to, and port to indicate which port we are connecting on, Flash requires us to use ports above 1024 for security reasons. Once the connection process has finished it will fire jsXMLSocket.onConnect.

jsXMLSocket.close()

Call this method to close the socket connection. It's important to know that this will not trigger the jsXMLSocket.onClose event.

jsXMLSocket.send(data)

Call this method to send the data in data. jsXMLSocket.send(data) will null terminate the data.

Events

jsXMLSocket.onReady()

Called by the socket when socket is ready for use.

jsXMLSocket.onConnect(success,error)

Called by the socket when socket has finished connecting. success will either be true when the connection is estabilished or false when it failed. error will contain the error text when the connection fails.

jsXMLSocket.onData(data)

Called by the socket when socket has received a null terminated message. data will contain the received data.

jsXMLSocket.onClose()

Called by the socket when socket has been closed on the other side.

Example

<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript" src="jsxmlsocket.js"></script>
    <script type='text/javascript'>
        // Host we are connecting to
        var host = 'localhost';
        // Port we are connecting on
        var port = 8008;

    var socket = new jsXMLSocket();

    // When the socket is added the to document
    socket.onReady = function() {
        socket.connect(host, port);
        $('#messages').append('Connecting to ' + host + ':' + port + '... ');
    }

    // Connection attempt finished
    socket.onConnect = function(success, error) {
        if(success) {
            socket.send('init');
            $('#messages').append('Connected.&lt;br/&gt;');
        } else {
            $('#messages').append('Connection failed.&lt;br/&gt;');
        }
    }

    // Server closed socket
    socket.onClose = function() {
        $('#messages').append('Server disconnected.&lt;br/&gt;');
    }

    // Received data
    socket.onData = function(data) {
        $('#messages').append(data + '&lt;br/&gt;');
    }

    $(document).ready(function() {
        // Setup our socket in the div with the id="socket"
        socket.setup('#socket');
    });
&lt;/script&gt;
&lt;title&gt;jsXMLSocket&lt;/title&gt;

</head> <body> <div id='socket'></div> <h1>jsXMLSocket</h1> <div id="messages"></div> </body> </html>

Adobe Flash security

Adobe Flash imposes security restrictions on socket access. To be able to use sockets in Flash the swf file must have been loaded from a webserver and not the local filsystem using a file:// url. Additionally the server you are connecting to must grant access to Flash before you can connect. This is done by sending a socket policy file in response to receiving a policy file request at port 843 or at the port you are trying to connect to. See Adobe's documentation on the Flash security restrictions.

Example socket policy for testing purposes:

<?xml version="1.0" encoding="UTF-8" ?> 
<cross-domain-policy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.adobe.com/xml/schemas/PolicyFileSocket.xsd">
    <allow-access-from domain="" to-ports="" secure="false" /> 
    <site-control permitted-cross-domain-policies="master-only" /> 
</cross-domain-policy>

Development

jsXMLSocket consists of only two source files: the Actionscript file jsxmlsocket.as and the javascript file jsxmlsocket.js. It is only necessary to change these two files to fix bugs and add functionality.

To compile a new swf file after modifying jsxmlsocket.as you need the Adobe Flex 3 SDK, then simply type:

mxmlc jsxmlsocket.as

Social media

  • delicious 11
  • ff_comments 1
  • twitter 9

Latest releases

You might also like...

Comments

Source Code Commits

  • 2. Initial checkin (11 years ago)
  • 2. Initial checkin (11 years ago)
  • 2. Initial checkin (11 years ago)
  • 2. Initial checkin (11 years ago)
  • 2. Initial checkin (11 years ago)

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“In order to understand recursion, one must first understand recursion.”