Module: WS

Defined in:
lib/websocket.rb

Constant Summary collapse

@@onMentionDo =
[false, nil]
@@debugging =
false
@@onReactionDo =
[false, nil]
@@onRenoteDo =
[false, nil]
@@onNotificationDo =
[false, nil]
@@onQuoteDo =
[false, nil]

Instance Method Summary collapse

Instance Method Details

#cObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
# File 'lib/websocket.rb', line 9

def c
  Thread.start do 
    EM.run do
      instance = "wss://#{Misskey.auth.instanceURI}/streaming?i=#{Misskey.auth.token}"
      socket = Faye::WebSocket::Client.new instance

      socket.onopen = lambda do |event|
        puts Rainbow("Connected to websocket successfully.").green
        socket.send '{"type":"connect","body":{"channel":"main","id":"1"}}'
      end

      socket.onmessage = lambda do |event|
        message = JSON.parse event.data

        if Misskey.debug.debugging
          puts "Got a websocket message!!1 The type is: #{message["body"]["type"]}"
        end

        case message["body"]["type"]
        when "notification"
          if Misskey.debug.debugging
            puts "The notification's note body type is: #{message["body"]["body"]["type"]}"
          end

          if @@onMentionDo[0] and message["body"]["body"]["type"] == "mention"
            @@onMentionDo[1].call message
          elsif @@onReactionDo[0] and message["body"]["body"]["type"] == "reaction"
            @@onReactionDo[1].call message
          elsif @@onRenoteDo[0] and message["body"]["body"]["type"] == "renote"
            @@onRenoteDo[1].call message
          elsif @@onQuoteDo[0] and message["body"]["body"]["type"] == "quote"
            @@onQuoteDo[1].call message
          end

          if @@onNotificationDo[0]
            @@onNotificationDo[1].call message
          end
        end
      end

      socket.onclose = lambda do |event|
        puts Rainbow("Connection to websocket closed.").red
      end

      socket.onerror = lambda do |event|
        puts "An error occurred: #{event}"
      end
    end
  end
end

#nObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/websocket.rb', line 60

def n 
  def oM block # onMention
    @@onMentionDo = [true, block]
  end

  def oR block # onReaction
    @@onReactionDo = [true, block]
  end

  def oRe block # onRenote
    @@onRenoteDo = [true, block]
  end

  def oN block # onNotification, like just any notification will trigger this.
    @@onNotificationDo = [true, block]
  end

  def oQ block # onQuote
    @@onQuoteDo = [true, block]
  end
end

#oM(block) ⇒ Object

onMention



61
62
63
# File 'lib/websocket.rb', line 61

def oM block # onMention
  @@onMentionDo = [true, block]
end

#oN(block) ⇒ Object

onNotification, like just any notification will trigger this.



73
74
75
# File 'lib/websocket.rb', line 73

def oN block # onNotification, like just any notification will trigger this.
  @@onNotificationDo = [true, block]
end

#oQ(block) ⇒ Object

onQuote



77
78
79
# File 'lib/websocket.rb', line 77

def oQ block # onQuote
  @@onQuoteDo = [true, block]
end

#oR(block) ⇒ Object

onReaction



65
66
67
# File 'lib/websocket.rb', line 65

def oR block # onReaction
  @@onReactionDo = [true, block]
end

#oRe(block) ⇒ Object

onRenote



69
70
71
# File 'lib/websocket.rb', line 69

def oRe block # onRenote
  @@onRenoteDo = [true, block]
end