'handling'에 해당되는 글 2건

  1. 2014.02.04 Web Socket API
  2. 2009.12.17 Redis Command Reference
2014. 2. 4. 19:13

Web Socket API

Web Socket API

The Web Socket protocol enables web applications to maintain bidirectional communications with server-side processes. The typhoonae.websocket package provides a Web Socket Service API for GAE applications. It was introduced by the TyphoonAE 0.1.2 release anddoes not run on the productive Google App Engine platform.

However, in order to enable the Web Socket API for the Google App Engine SDK, go through the following guide.

Patching the Google App Engine SDK (Python) to enable TyphoonAE's Web Socket API

To get an idea how Web Sockets could work with GAE download the SDK 1.5.0 patch from this location. It also includes a demo app and some basic instructions.

Web Borwsers Supporting Web Sockets

Overview

Since Bret Taylor came up with a neat implementation of a Web Socket handler for the Tornado web server, it's no longer a hassle to get an experimental Web Socket service up and running. A client can establish a Web Socket connection to the service which solely utilizes web hooks to dispatch messages to and from an application.


A client requests (1) a web page containing the Java Script call to establish a Web Socket connection (2) to a URL provided by the application. The application and Web Socket service communicate over web hooks (3, 4).

Sending and Receiving Web Socket Messages

We distinguish between two types of incoming Web Socket messages. A handshake message is received once per Web Socket session when the client establishes the connection. This type of message can be handled seperately by a request handler. All other incoming messages can be handled by another request handler. The API we use in our handshake handler as well as in the handler for all other incoming Web Socket messages doesn't differ. However, in many cases it is very useful to handle Web Socket handshake messages seperately from normalmessages.

In order to handle incoming Web Socket requests, add this script handler to the handlers section of the app.yaml file.

  - url: /_ah/websocket/.*
    script
: script.py
    login
: admin

Define the correct URL mapping for your WSGI application:

  app = google.appengine.ext.webapp.WSGIApplication([
   
('/_ah/websocket/handshake/(.*)', HandshakeHandler),
   
('/_ah/websocket/message/(.*)', MessageHandler),
 
], debug=True)

Our Web Socket API provides a convenience function to obtain the appropriate service URL. It has one argument for additional URI information:

  websocket_url = typhoonae.websocket.create_websocket_url('/foo/bar')

The following request handler receives and sends messages from and to a Web Socket. The POST method is used to receive an incoming message where the first non-self argument contains the additional URI information from above:

  class MessageHandler(google.appengine.ext.webapp.RequestHandler):
   
"""Handles Web Socket requests."""

   
def post(self, path):
      message
= typhoonae.websocket.Message(self.request.POST)
      typhoonae
.websocket.send_message(
       
[message.socket], 'Received: "%s"' % message.body)

message object has the two attributes socket and body. The former is a string containing the socket id. The latter contains our message body as a unicode string.

The client, usually a Web Socket capable browser, establishes a Web Socket by using Java Script:

  ws = new WebSocket("ws://example.com");

Broadcast Messages

TyphoonAE adds another very useful function which enables an app to broadcast messages to all currently open Web Sockets of an app. Without this convenient method a developer has to implement a solution to remember all open Web Sockets. By using broadcast_message the Web Socket service takes care of it.

  class MessageHandler(google.appengine.ext.webapp.RequestHandler):
   
"""Handles Web Socket requests."""

   
def post(self, path):
      message
= typhoonae.websocket.Message(self.request.POST)
      typhoonae
.websocket.broadcast_message(message.body)

Handling Closed Sockets

Applications sometimes should be informed when a socket is closed. Therefore, an app can implement a third request handler for the following URL pattern:

    '/_ah/websocket/closed/(.*)'

The success path can be utilized for keeping track of additional informations such as encoded user IDs.

  class SocketClosedHandler(google.appengine.ext.webapp.RequestHandler):
   
"""Handler for socket closed events."""

   
def post(self, path):
      user
= decode_user_from_path(path)
      typhoonae
.websocket.broadcast_message('%s has left the building' % user)

See http://dev.w3.org/html5/websockets/ for further information on Web Sockets.




출처 : code.google.com


Trackback 0 Comment 0
2009. 12. 17. 19:50

Redis Command Reference

memcached 류의 Key-Value 모델 이상의 역할을 수행하는 것이 가능하다.

PHP와 Redis만으로 (즉, 별도의 Database 등을 사용하지 않고) 개발되고 있다.

 

Every command name links to a specific wiki page describing the behavior of the command.

Connection handling

  • QUIT close the connection
  • AUTH simple password authentication if enabled

Commands operating on all the kind of values

  • EXISTS key test if a key exists
  • DEL key delete a key
  • TYPE key return the type of the value stored at key
  • KEYS pattern return all the keys matching a given pattern
  • RANDOMKEY return a random key from the key space
  • RENAME oldname newname rename the old key in the new one, destroing the newname key if it already exists
  • RENAMENX oldname newname rename the old key in the new one, if the newname key does not already exist
  • DBSIZE return the number of keys in the current db
  • EXPIRE set a time to live in seconds on a key
  • TTL get the time to live in seconds of a key
  • SELECT index Select the DB having the specified index
  • MOVE key dbindex Move the key from the currently selected DB to the DB having as index dbindex
  • FLUSHDB Remove all the keys of the currently selected DB
  • FLUSHALL Remove all the keys from all the databases

Commands operating on string values

  • SET key value set a key to a string value
  • GET key return the string value of the key
  • GETSET key value set a key to a string returning the old value of the key
  • MGET key1 key2 ... keyN multi-get, return the strings values of the keys
  • SETNX key value set a key to a string value if the key does not exist
  • MSET key1 value1 key2 value2 ... keyN valueN set a multiple keys to multiple values in a single atomic operation
  • MSETNX key1 value1 key2 value2 ... keyN valueN set a multiple keys to multiple values in a single atomic operation if none of the keys already exist
  • INCR key increment the integer value of key
  • INCRBY key integer increment the integer value of key by integer
  • DECR key decrement the integer value of key
  • DECRBY key integer decrement the integer value of key by integer

Commands operating on lists

  • RPUSH key value Append an element to the tail of the List value at key
  • LPUSH key value Append an element to the head of the List value at key
  • LLEN key Return the length of the List value at key
  • LRANGE key start end Return a range of elements from the List at key
  • LTRIM key start end Trim the list at key to the specified range of elements
  • LINDEX key index Return the element at index position from the List at key
  • LSET key index value Set a new value as the element at index position of the List at key
  • LREM key count value Remove the first-N, last-N, or all the elements matching value from the List at key
  • LPOP key Return and remove (atomically) the first element of the List at key
  • RPOP key Return and remove (atomically) the last element of the List at key
  • RPOPLPUSH srckey dstkey Return and remove (atomically) the last element of the source List stored at _srckey_ and push the same element to the destination List stored at _dstkey_

Commands operating on sets

  • SADD key member Add the specified member to the Set value at key
  • SREM key member Remove the specified member from the Set value at key
  • SPOP key Remove and return (pop) a random element from the Set value at key
  • SMOVE srckey dstkey member Move the specified member from one Set to another atomically
  • SCARD key Return the number of elements (the cardinality) of the Set at key
  • SISMEMBER key member Test if the specified value is a member of the Set at key
  • SINTER key1 key2 ... keyN Return the intersection between the Sets stored at key1, key2, ..., keyN
  • SINTERSTORE dstkey key1 key2 ... keyN Compute the intersection between the Sets stored at key1, key2, ..., keyN, and store the resulting Set at dstkey
  • SUNION key1 key2 ... keyN Return the union between the Sets stored at key1, key2, ..., keyN
  • SUNIONSTORE dstkey key1 key2 ... keyN Compute the union between the Sets stored at key1, key2, ..., keyN, and store the resulting Set at dstkey
  • SDIFF key1 key2 ... keyN Return the difference between the Set stored at key1 and all the Sets key2, ..., keyN
  • SDIFFSTORE dstkey key1 key2 ... keyN Compute the difference between the Set key1 and all the Sets key2, ..., keyN, and store the resulting Set at dstkey
  • SMEMBERS key Return all the members of the Set value at key
  • SRANDMEMBER key Return a random member of the Set value at key

Commands operating on sorted sets (zsets, Redis version >= 1.1)

  • ZADD key score member Add the specified member to the Set value at key or update the score if it already exist
  • ZREM key member Remove the specified member from the Set value at key
  • ZRANGE key start end Return a range of elements from the sorted set at key
  • ZREVRANGE key start end Return a range of elements from the sorted set at key, exactly like ZRANGE, but the sorted set is ordered in traversed in reverse order, from the greatest to the smallest score
  • ZRANGEBYSCORE key min max Return all the elements with score >= min and score <= max (a range query) from the sorted set
  • ZCARD key Return the cardinality (number of elements) of the sorted set at key
  • ZSCORE key element Return the score associated with the specified element of the sorted set at key
  • ZREMRANGEBYSCORE key min max Remove all the elements with score >= min and score <= max from the sorted set

Sorting

  • SORT key BY pattern LIMIT start end GET pattern ASC|DESC ALPHA Sort a Set or a List accordingly to the specified parameters

Persistence control commands

  • SAVE Synchronously save the DB on disk
  • BGSAVE Asynchronously save the DB on disk
  • LASTSAVE Return the UNIX time stamp of the last successfully saving of the dataset on disk
  • SHUTDOWN Synchronously save the DB on disk, then shutdown the server
  • BGREWRITEAOF Rewrite the append only file in background when it gets too big

Remote server control commands

  • INFO Provide information and statistics about the server
  • MONITOR Dump all the received requests in real time
  • SLAVEOF Change the replication settings

출처 : http://code.google.com/p/redis

Trackback 0 Comment 0