Package core :: Module sock_js |
[hide private]
[frames] | no frames]

# Source Code for Module core.sock_js

      1  # Copyright 2011 the original author or authors.
      2  #
      3  # Licensed under the Apache License, Version 2.0 (the "License");
      4  # you may not use this file except in compliance with the License.
      5  # You may obtain a copy of the License at
      6  #
      7  #      http://www.apache.org/licenses/LICENSE-2.0
      8  #
      9  # Unless required by applicable law or agreed to in writing, software
     10  # distributed under the License is distributed on an "AS IS" BASIS,
     11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12  # See the License for the specific language governing permissions and
     13  # limitations under the License.
     14
     15  import core.streams
     16  import core.ssl_support
     17  import org.vertx.java.core
     18  import org.vertx.java.core.json
     19  import org.vertx.java.deploy.impl
     20
     21  from core.javautils import map_to_java
     22  from core.event_bus import EventBus
     23
     24  __author__ = "Scott Horn"
     25  __email__ = "scott@hornmicro.com"
     26  __credits__ = "Based entirely on work by Tim Fox http://tfox.org"
     27
     28 -class SockJSServer(object):
     29      """This is an implementation of the server side part of https://github.com/sockjs
     30
     31      SockJS enables browsers to communicate with the server using a simple WebSocket-like api for sending
     32      and receiving messages. Under the bonnet SockJS chooses to use one of several protocols depending on browser
     33      capabilities and what apppears to be working across the network.
     34
     35      Available protocols include:
     36
     37      WebSockets
     38      xhr-polling
     39      xhr-streaming
     40      json-polling
     41      event-source
     42      html-file
     43
     44      This means, it should just work irrespective of what browser is being used, and whether there are nasty
     45      things like proxies and load balancers between the client and the server.
     46
     47      For more detailed information on SockJS, see their website.
     48
     49      On the server side, you interact using instances of SockJSSocket - this allows you to send data to the
     50      client or receive data via the ReadStream data_handler.
     51
     52      You can register multiple applications with the same SockJSServer, each using different path prefixes, each
     53      application will have its own handler, and configuration is described in a Hash.
     54      """
     55
     56 -    def __init__(self, http_server):
     57        self.java_obj = org.vertx.java.deploy.impl.VertxLocator.vertx.createSockJSServer(http_server._to_java_server())
     58
     59 -    def install_app(self, config, handler):
     60          """Install an application
     61
     62          Keyword arguments:
     63          @param config: Configuration for the application
     64          @param proc: Proc representing the handler
     65          @param handler: Handler to call when a new SockJSSocket is created
     66          """
     67          java_config = org.vertx.java.core.json.JsonObject(map_to_java(config))
     68          self.java_obj.installApp(java_config, SockJSSocketHandler(handler))
     69
     70 -    def bridge(self, config, inbound_permitted, outbound_permitted, auth_timeout=5*60*1000, auth_address=None):
     71          a_ijson = org.vertx.java.core.json.JsonArray(map_to_java(inbound_permitted))
     72          a_ojson = org.vertx.java.core.json.JsonArray(map_to_java(outbound_permitted))
     73          self.java_obj.bridge(org.vertx.java.core.json.JsonObject(map_to_java(config)), a_ijson, a_ojson, auth_timeout, auth_address)
     74
     75 -class SockJSSocket(core.streams.ReadStream, core.streams.WriteStream):
     76      """You interact with SockJS clients through instances of SockJS socket.
     77      The API is very similar to WebSocket. It implements both
     78      ReadStream and WriteStream so it can be used with Pump to enable
     79      flow control.
     80      """
     81
     82 -    def __init__(self, java_sock):
     83          self.java_obj = java_sock
     84
     85          def simple_handler(msg):
     86              self.write_buffer(msg.body)
     87
     88          self.handler_id = EventBus.register_simple_handler(True, simple_handler)
     89
     90 -    def close(self):
     91          """Close the socket"""
     92          EventBus.unregister_handler(self.handler_id)
     93          self.java_obj.close()
     94
     95 -    def handler_id(self):
     96          """When a SockJSSocket is created it automatically registers an event handler with the system, the ID of that
     97          handler is given by handler_id.
     98          Given this ID, a different event loop can send a buffer to that event handler using the event bus. This
     99          allows you to write data to other SockJSSockets which are owned by different event loops.
    100          """
    101          return self.handler_id
    102
    103 -    def _to_java_socket(self):
    104        return self.java_obj
    105
    106 -class SockJSSocketHandler(org.vertx.java.core.Handler):
    107      """ SockJS Socket handler """
    108 -    def __init__(self, handler):
    109          self.handler = handler
    110
    111 -    def handle(self, sock):
    112          """ Call the handler after SockJS Socket is ready"""
    113          self.handler(SockJSSocket(sock))
    114