1
2
3
4
5
6
7
8
9
10
11
12
13
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
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
58
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):
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
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
94
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
105
107 """ SockJS Socket handler """
109 self.handler = handler
110
112 """ Call the handler after SockJS Socket is ready"""
113 self.handler(SockJSSocket(sock))
114