Archive for October, 2005

Simple AJAX (with JSON) Chat Application for ASP 3.0

The code in this package (not only a snapshot, but also a darcs repository) was originally developed for two reasons: for use in teaching a course in Javascript as part of a guest lecture series for the University of Westminster, and as a consequence of my personal interest in experimenting with modern client-server programming techniques.

It’s a very, very simple chat application using XmlHttpRequest to communicate in an AJAX style with an ASP 3.0 server that processes JSON-RPC-like requests and manages a simple shared database.

The reason ASP 3.0 was chosen is because it was the server-side environment in place at the University laboratories for the course I was teaching. Programming in JavaScript on both the client- and server-sides simultaneously was a pleasant experience; modern environments such as Helma have a lot going for them.

If you take a look at the code, you’ll see that I use the term “AJAJ” often: the idea is to take the core elements of AJAX, but replace the unwieldy XML with something lighter-weight and better fitted to Javascript programming. In this case, the “X” for XML is replaced by “J” for JSON, which is a lightweight data language that is particularly convenient for working with Javascript.

Code overview

The application depends on a few third-party libraries that make programming in Javascript a little more pleasant. These are the parts of the application that I wrote:

  • simple-chat-client.html: Contains the HTML user-interface to the service, and the basic client-side Javascript code for driving the specifics of the chat service.

  • chat-server/simple-chat.asp: Contains the server-side code responsible for responding to the client’s RPC requests for chat-server functionality. Does not itself present any HTML at all: it only speaks AJAJ, accepting JSON requests and replying with JSON responses.

  • js/ajaj.js: Implements the client side of the AJAJ RPC library, building upon json.js.

  • chat-server/ajaj.asp: Implements the server side of the AJAJ RPC library, building also upon json.js (in the form of json.asp). The only part of this file not authored by LShift is the contained VBScript function BinaryToString, which was written by Antonin Foller and comes from http://www.pstruh.cz.

  • js/console.js: A reusable Javascript DHTML evaluator. Useful for debugging and interactive development of Javascript-bearing HTML pages.

  • js/util.js: A collection of utilities lending, among other things, a functional-programming flavour to Javascript.

  • chat-server/jsondb.asp: A simple (toy) transactional database driver for ASP 3.0 using the filesystem as a data store.

The following files are mostly library code from third parties:

5 comments October 11th, 2005 tonyg

pregexp over streams, and other random hackery

Over at eighty-twenty I recount a couple of recent random excursions into various bits and pieces of code.

Besides those developments, I also spent some time on Sunday morning modifying Dorai Sitaram’s pregexp version 20050502 to operate over streams as well as strings, so that I could use it for lexing arbitrary character sources (for instance, with the packrat parser library I’ve been developing).

The basic interface, after the patch, is now either the standard

(pregexp-match <pattern> <string>)

or the new

(pregexp-match <pattern> <stream>)

where streams are created with

(pregexp-make-stream <generatorfunction> <seed>)

I’ve also added a procedure pregexp-match-head, which is like pregexp-match-positions except it only matches at the very beginning of the input string or stream; pregexp-match-head behaves like Python’s re.match, where pregexp-match-positions behaves like Python’s re.search.

I haven’t modified pregexp-split, pregexp-replace, or pregexp-replace*, partly because I have no need for them for my application and partly because I’m not sure what their behaviour should be: should pregexp-replace, when given a stream, answer a stream, or a string? In the case of pregexp-split, since it has to examine the entire input in any case, supporting streams seems unnecessary. (Perhaps I should have included a pregexp-stream->string utility, though.)

The patch against version 20050502 is downloadable here.

Thanks to Dorai for a great library!

Update: I noticed a bug in the first revision of the patch. I’ve updated the links in the article above to point to the new patch.

Add comment October 11th, 2005 tonyg

Next Posts

Calendar

October 2005
M T W T F S S
« Sep   Nov »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Posts by Month

Posts by Category