Module: BitShares::WSocket

Defined in:
lib/bitshares/wsocket.rb

Class Method Summary collapse

Class Method Details

.get_noticeObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bitshares/wsocket.rb', line 64

def get_notice
	if msgs.key?('notice') && !@msgs['notice'].nil?
		msg = @msgs['notice']
		lock.synchronize do
			@msgs['notice'] = nil
		end
		msg
	else
		nil
	end
end

.lockObject



62
# File 'lib/bitshares/wsocket.rb', line 62

def lock() @lock ||= Mutex.new end

.msgsObject



26
# File 'lib/bitshares/wsocket.rb', line 26

def msgs() @msgs ||= {} end

.send(hash) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bitshares/wsocket.rb', line 32

def send hash
	id = hash[:id]
	count = 0
	while msgs[id] != nil do
		sleep 0.1
		count += 1
		raise "TimeoutError..." if count > 100
	end

	if @status == :connected
		@thread[:ws].send hash.to_json
	else
		return nil
	end

	count = 0
	while @msgs[id].nil? and @status == :connected do
		sleep 0.1
		count += 1
		raise "TimeoutError..." if count > 100
	end

	result = @msgs[id]
	lock.synchronize do
		@msgs[id] = nil
	end
	raise("Bad request...#{result}") if result.nil? || result.key?('error')
	result['result']
end

.startObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/bitshares/wsocket.rb', line 8

def start
	@msgs = {}
	@thread ||= Thread.new { run }
	Thread.new do
		while @status != :disconnected do
			@thread[:ws].ping 'Hi' if @status == :connected
			sleep 50
		end
	end
	count = 0
	while @status != :connected and @status != :disconnected do
		sleep 0.1
		count += 1
		raise "TimeoutError..." if count > 100
	end
end

.stopObject



28
29
30
# File 'lib/bitshares/wsocket.rb', line 28

def stop
	EventMachine.stop
end

.threadObject



25
# File 'lib/bitshares/wsocket.rb', line 25

def thread() @thread end