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]
@@onReplyDo =
[false, nil]
@@onMentionOrReplyDo =
[false, nil]
@@onClose =
[false, nil]
@@onError =
[false, nil]
@@onOpen =
[false, nil]
@@onMessage =
[false, nil]

Instance Method Summary collapse

Instance Method Details

#cObject



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/websocket.rb', line 15

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"}}'

        if @@onOpen[0]
          @@onOpen[1].call event
        end
      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"
          begin
            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
            elsif @@onReplyDo[0] and message["body"]["body"]["type"] == "reply"
              @@onReplyDo[1].call message
            elsif @@onMentionOrReplyDo[0] and /(reply)|(mention)/.match message["body"]["body"]["type"]
              @@onMentionOrReplyDo[1].call message
            end

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

            if @@onMessage[0]
              @@onMessage[1].call event
            end
          rescue => error
            puts Rainbow("An error occurred in the websocket event: ").red + error.to_s
          end
        end
      end

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

        if @@onClose[0]
          @@onClose[1].call event
        end
      end

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

        if @@onError[0]
          @@onError[1].call event
        end
      end
    end
  end
end

#eObject

Event



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/websocket.rb', line 120

def e # Event
  def oC block # onClose
    @@onClose = [true, block]
  end

  def oE block # onError
    @@onError = [true, block]
  end

  def oO block # onOpen
    @@onOpen = [true, block]
  end

  def oM block # onMessage
    @@onMessage = [true, block]
  end
end

#nObject

Notifications



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/websocket.rb', line 90

def n # Notifications
  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

  def oRep block # onReply
    @@onReplyDo = [true, block]
  end

  def oMOR block # onMentionOrReply
    @@onMentionOrReplyDo = [true, block]
  end
end

#oC(block) ⇒ Object

onClose



121
122
123
# File 'lib/websocket.rb', line 121

def oC block # onClose
  @@onClose = [true, block]
end

#oE(block) ⇒ Object

onError



125
126
127
# File 'lib/websocket.rb', line 125

def oE block # onError
  @@onError = [true, block]
end

#oM(block) ⇒ Object

onMessage



91
92
93
# File 'lib/websocket.rb', line 91

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

#oMOR(block) ⇒ Object

onMentionOrReply



115
116
117
# File 'lib/websocket.rb', line 115

def oMOR block # onMentionOrReply
  @@onMentionOrReplyDo = [true, block]
end

#oN(block) ⇒ Object

onNotification, like just any notification will trigger this.



103
104
105
# File 'lib/websocket.rb', line 103

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

#oO(block) ⇒ Object

onOpen



129
130
131
# File 'lib/websocket.rb', line 129

def oO block # onOpen
  @@onOpen = [true, block]
end

#oQ(block) ⇒ Object

onQuote



107
108
109
# File 'lib/websocket.rb', line 107

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

#oR(block) ⇒ Object

onReaction



95
96
97
# File 'lib/websocket.rb', line 95

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

#oRe(block) ⇒ Object

onRenote



99
100
101
# File 'lib/websocket.rb', line 99

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

#oRep(block) ⇒ Object

onReply



111
112
113
# File 'lib/websocket.rb', line 111

def oRep block # onReply
  @@onReplyDo = [true, block]
end