Archive

Tag Archives: javascript

If you have some flash application talking to back-end via TCP, then it would be useful for you to launch special cross domain policy server on 843 port:

sudo bash -c “exec nohup setsid node policy_server.js 0<&- &”

// policy_server.js
var server = require(“net”).createServer(function(socket) {
    socket.on(‘error’, function(err) {});
    socket.end(‘<cross-domain-policy><allow-access-from domain=”*” to-ports=”*”/></cross-domain-policy>’);
});
server.on(‘error’, function(err) {});
server.listen(843);

Some time ago I designed and implemented mobile solution to browse/search legal information actual in the Netherlands. There are a lot of legal institutions, rules, etc. in the Netherlands, so, the idea of application is to provide the roadmap for regular user to solve specified law question/problem.

There is a client-server solution. From one hand there is free android application which can be used as offline manual. As it’s mostly content oriented application, I created it by PhoneGap + HTML5 + jQueryMobile technology. But it’s not pure HTML5/JS/CSS solution because it needs several PhoneGap plugins implemented by Java.

Server side of the solution is a simple PHP+MySQL CMS implemented as single-page backbone.js web application to compose and correct the content of the manual. I also used Slim MVC, Twig templates and idiorm ORM to MySQL frameworks.

The workflow is:

  1. Content manager edits the content (CRUD) and deploy new version available via HTTP online;
  2. The clients automatically update (check, download and install) the latest version of content from that HTTP interface when they are online;
  3. Then the clients may use new content even offline.

There are also such useful features:

  • search by tags
  • feedback form
  • share the link/content via email/facebook/twitter/etc.

Image

Yesterday Alexander Orlov mentioned interesting mind exercise: using mouse by left hand (if you use it by right one on regular basis). This trick stimulates the right hemisphere to force our mind to generate even more crazy ideas. After first day of such left-mouse clicking I created simple but fun html5 down counter in the address bar of the browser (isn’t that fun?). I tested it in FF, Chrome, Opera and Safari. It doesn’t work in IE (means html5 is still dangerous territory or IE must die, underline your own).

The best developer is the professional who try to automate as many tasks as possible. Yesterday I was involved into such automation process. There was the sheet of play cards and I needed to split it into the single card images and import into .fla flash as resource for flash game project. So, I implement Automation even twice.

First task

Spilt sheet into several images. For this first task I used Python and PIL:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import PIL
from PIL import Image
im_source = Image.open(“classic-playing-cards.png”)
width = 71
height = 96
ys = (‘c’, ‘h’, ‘s’, ‘d’)
xs = (‘a’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘t’, ‘j’, ‘q’, ‘k’)
for x in xrange(0, 13):
    for y in xrange(0 ,4):
        file_name = “%s%s.png” % (xs[x], ys[y])
        im_c = im_source.copy()
        left = x * (width+2) + 1
        top = y * (height+2) + 1
        im_c = im_source.crop((left+1, top+1, left + width+1, top + height+1))
        im_c.save(file_name)
        print file_name

52 cards. So, this script applying is much faster and safer way than photoshop.

Second task

I imported 52 cards to Flash IDE. And now it needs to generate 52 sprites (one per each card). I used JSFL (JavaScript for Flash) for that:

var doc = fl.getDocumentDOM();
var lib = doc.library;

var items = lib.getSelectedItems();

var prefix = “com.kikudjiro.minigames.blackjack.cards.C”;
var ys = [‘a’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘t’, ‘j’, ‘q’, ‘k’];
var xs = [‘c’, ‘d’, ‘h’, ‘s’];

for (var i in items)
{
     var bitmap = items[i];
     var sX = bitmap.name.substring(15, 16);
     var sY = bitmap.name.substring(14, 15);
     var suffix = ys.indexOf(sY) * 4 + xs.indexOf(sX);
     if (suffix < 10)
          suffix = ‘0’ + suffix;
     var card_name = prefix + suffix;

           lib.addNewItem(“movieclip”, card_name);
     var index = lib.findItemIndex(card_name);
     var item = lib.items[index];
     item.linkageExportForAS = true;
     item.linkageExportInFirstFrame = true;
     item.linkageBaseClass = “flash.display.Sprite”;
     item.linkageClassName = card_name;

           lib.editItem(card_name);
     lib.addItemToDocument({x:0, y:0}, “cards/bitmaps/” + sY + sX + “.png”);

           lib.moveToFolder(“cards/deck”, card_name);
}

P.S.
The most useful automation scenario is using FAR manager macros system. Press Ctr-. to start macro recording. And after apply some set of regular actions that should be repeated press Ctr-. to stop recording and set hot key for the macro. Usually I practice it in internal FAR text editor but it’s ok to use it for any FAR actions.

Several months ago I was involved into discussion about the best way to implement realtime light-weight web chat like FB or GTalk right in Web.
In way like Comet or WebSocket. And now I have prety simple but robust solution. I’m talking about node.js + socket.io.

There is working example (not even pure chat but rather web echo service). To try it you need to install node.js, npm and then socket.io by npm. Then just run sudo node echo_server.js and go to http://localhost:8000.

Linux needs root’s privileges to server correctly flash client as bridge (one of transport layers provided by socket.io) on port 843.

One more interesting feature in this example is auto-scroll-bottom widget on client side like real chat messages box.

Basically, this is just test example or start point to implement real chat service or multiplayer game.