Module: Tri
- Defined in:
- lib/tri.rb
Class Method Summary collapse
-
.get_client ⇒ Object
Accept the connection from client.
-
.get_client_with_keep_alive(second) ⇒ Object
Accept the connection from client with a keep alive.
-
.is_running? ⇒ Boolean
Check if the server is till running.
-
.ping_to(client) ⇒ Object
Send a PING frame to client.
-
.receive_from(client) ⇒ Object
Trying to get data from client.
-
.send_to(client, data) ⇒ Object
Send data to client.
-
.start(input1 = "localhost", input2 = 888) ⇒ Object
Start a WetSocket with TCP/IP layer.
Class Method Details
.get_client ⇒ Object
Accept the connection from client
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/tri.rb', line 34 def self.get_client if is_running? client = @server.accept # Got a connection from a client if key = validWebSocketRequest?(client) # Send the respone back for handshaking phase client.print caculateRequestReply(key) else # Close the connection due to invalid WebSocket requests client.close client = nil end end # Return the client client end |
.get_client_with_keep_alive(second) ⇒ Object
Accept the connection from client with a keep alive
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/tri.rb', line 54 def self.get_client_with_keep_alive(second) if is_running? client = @server.accept # Got a connection from a client if key = validWebSocketRequest?(client) # Send the respone back for handshaking phase client.print caculateRequestReply(key) # Sending PING, for every 28 sec Thread.new { while client ping_to(client) sleep(second) end } else # Close the connection due to invalid WebSocket requests client.close client = nil end end # Return the client client end |
.is_running? ⇒ Boolean
Check if the server is till running
29 30 31 |
# File 'lib/tri.rb', line 29 def self.is_running? !@server.nil? end |
.ping_to(client) ⇒ Object
Send a PING frame to client
100 101 102 103 104 105 |
# File 'lib/tri.rb', line 100 def self.ping_to(client) pingData = "CHAT4NINJA" firstByte = "10001001".to_i(2).chr secondByte = pingData.size.chr client.write(firstByte + secondByte + pingData) end |
.receive_from(client) ⇒ Object
Trying to get data from client
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/tri.rb', line 81 def self.receive_from(client) data = getData(client) case data when -2 [-2,"[ERROR] Huge payload - should close the connection."] when -1 [-2,"[ERROR] No data due to connection timeout."] when 8 [8, "[CLOSE] Close frame sent from the client."] when 9 [8, "[OPCODE] PING frame sent from the client."] when 10 [9, "[OPCODE] PONG frame sent from the client."] else [1, data] end end |
.send_to(client, data) ⇒ Object
Send data to client
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/tri.rb', line 108 def self.send_to(client,data) firstByte = "10000001".to_i(2).chr if data.size <=127 secondByte = data.size.chr client.write(firstByte + secondByte + data) else binaryString= data.size.to_s(2) extraZero = "" (16 - binaryString.size).times do extraZero += "0" end extraZero = extraZero + binaryString secondByte = "01111110".to_i(2).chr thirdByte = extraZero[0..7].to_i(2).chr forthByte = extraZero[8..15].to_i(2).chr begin client.write(firstByte + secondByte + thirdByte + forthByte + data) # Catch err raised by the command above rescue SystemCallError => err # Return error code "[ERROR] Trying to send {" + firstByte + secondByte + thirdByte + forthByte + data + "} " + err.to_s else # Return success message end end end |
.start(input1 = "localhost", input2 = 888) ⇒ Object
Start a WetSocket with TCP/IP layer
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/tri.rb', line 6 def self.start(input1="localhost", input2 = 888) # Check input parameters if !input1.is_a? String hostname = "localhost" port = input1 else hostname = input1 port = input2 end # Trying to init a new TCPserver begin @server = TCPServer.new(hostname,port) # Catch err raised by the command above rescue SystemCallError => err # Return error code "[ERROR] Trying to start the server @" + hostname + ":" + port.to_s + ".\r\n[ERROR] " + err.to_s else # Return success message "[SERVER] The server is now listening @" + hostname + ":" + port.to_s + "." end end |