Class: QWebChannel

Inherits:
Object
  • Object
show all
Defined in:
lib/QWebChannel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(websocket, &block) ⇒ QWebChannel

def handlePropertyUpdate(message)



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/QWebChannel.rb', line 125

def initialize(websocket, &block)
    @index = nil #索引对象。
    @execId=0
    @execCallbacks={}
    @objects={}
    
    @transport=websocket
    @channel = self
    
    
    @transport.onmessage do |msg, type|
#             puts "Received message: #{msg}"
        
        puts "msg type: #{msg.class}"
        
        data =msg.to_s
        
        puts "data: #{data}"
        
        if (data.is_a?(String))
            data=JSON.parse(data)
        end
        
        puts "type: #{data.class}"
        
        case(data['type'])
        when QWebChannelMessageTypes::SIGNAL
            @channel.handleSignal(data)
        when QWebChannelMessageTypes::RESPONSE
            @channel.handleResponse(data)
        when QWebChannelMessageTypes::PROPERTYUPDATE
            @channel.handlePropertyUpdate(data)
        else
            puts("Invalid message arrived: #{data}")
        end
    end
    
    @channel.exec({'type' => QWebChannelMessageTypes::INIT}) do |data|
        puts "data type: #{data.class}"
        puts "data content: #{data}"
        
        data.each do |objectName, objectContent|
            puts "objectName type: #{objectName.class}"

            object=QObject.new(objectName, data[objectName], @channel)
        end
        
        @channel.objects.each do |objectName, objectObject| #一个个地解析出属性。
            @channel.objects[objectName].unwrapProperties()
        end #@channel.objects.each do |objectName, objectObject| #一个个地解析出属性。
        
        
#             puts "Self: #{self}"
        yield(self)
        
        
        @channel.exec({ "type" => QWebChannelMessageTypes::IDLE})
    end #@channel.exec({'type' => QWebChannelMessageTypes::INIT}) do |data|
end

Instance Attribute Details

#execCallbacksObject

Returns the value of attribute execCallbacks.



66
67
68
# File 'lib/QWebChannel.rb', line 66

def execCallbacks
  @execCallbacks
end

#execIdObject

Example:

>> #!/usr/bin/env ruby
>> 
>> require 'websocket-eventmachine-client'
>> require 'QWebChannel'
>> 
>> $core={}
>> 
>> $showReceivedMessageFromServer=Proc.new do |messageFromServer|
>>     puts "Received message from server: #{messageFromServer}"
>> end #$showReceivedMessageFromServer=Proc.new do |messageFromServer|
>> 
>> EM.run do
>>     ws = WebSocket::EventMachine::Client.connect(:uri => 'ws://localhost:12345')
>> 
>>     ws.onopen do
>>         qWebChannel = QWebChannel.new(ws) do |channel|
>>             $core=channel.objects['core']
>>             
>>             puts "Connected to WebChannel, ready to send/receive messages!"
>>             
>>             puts "Sending message 'hello from client' to the server."
>>             $core.receiveText('hello from client') #Call the "receiveText" slot on the server side.
>> 
>>             $core['sendText'].connect($showReceivedMessageFromServer) #Connect to the "sendText" signal on the server side.
>>         end #qWebChannel = QWebChannel.new(ws) do |channel|
>>     end #ws.onopen do
>> 
>>     ws.onclose do |code, reason|
>>         puts "Disconnected with status code: #{code}"
>>         EM.stop
>>     end
>> end #EM.run do
>>


64
65
66
# File 'lib/QWebChannel.rb', line 64

def execId
  @execId
end

#objectsObject

Returns the value of attribute objects.



70
71
72
# File 'lib/QWebChannel.rb', line 70

def objects
  @objects
end

#transportObject

Returns the value of attribute transport.



68
69
70
# File 'lib/QWebChannel.rb', line 68

def transport
  @transport
end

Instance Method Details

#exec(data, &block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/QWebChannel.rb', line 83

def exec(data, &block)
    if !(block_given?)
        @channel.send(data)
        return
    end
    
    @channel.execId=@channel.execId+1
    data['id']=@channel.execId
            
    puts "data id: #{data['id']}"

    @channel.execCallbacks[data['id']]=block
    @channel.send(data)
end

#handlePropertyUpdate(message) ⇒ Object

def handleSignal(message)



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/QWebChannel.rb', line 112

def handlePropertyUpdate(message)
    message['data'].each do |data| #一个个数据地处理。
        object= @channel.objects[data['object']] #获取发生变更的对象。
        
        if (object) #对象存在。
            object.propertyUpdate(data['signals'], data['properties']) #更新属性。
        else #对象不存在。
        end #if (object) #对象存在。
    end #message['data'].each do |data| #一个个数据地处理。
    
    @channel.exec({ 'type' => QWebChannelMessageTypes::IDLE })
end

#handleResponse(message) ⇒ Object



98
99
100
101
102
# File 'lib/QWebChannel.rb', line 98

def handleResponse(message)
    @channel.execCallbacks[message['id']].call(message['data'])
    
    @channel.execCallbacks[message['id']]=nil
end

#handleSignal(message) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/QWebChannel.rb', line 104

def handleSignal(message)
    object=@channel.objects[message['object']]
    
    if (object)
        object.signalEmitted(message['signal'], message['args'])
    end
end

#send(data) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/QWebChannel.rb', line 73

def send(data)
    if ( !(   data.is_a?(String) ) )
        data=Oj.dump(data)
    end
    
    puts "Sending: #{data}"
    
    @channel.transport.send(data)
end