Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions lib/fluent/plugin/out_websocket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
require 'em-websocket'
require 'thread'


class EventMachine::WebSocket::Connection
attr_accessor :tags
end

module Fluent
$lock = Mutex::new
$channel = EM::Channel.new
Expand All @@ -34,8 +39,19 @@ def configure(conf)
$log.info "WebSocket server #{@host}:#{@port} [msgpack: #{@use_msgpack}]"
EM.run {
EM::WebSocket.run(:host => @host, :port => @port) do |ws|
ws.tags = []
ws.onopen { |handshake|
callback = @use_msgpack ? proc{|msg| ws.send_binary(msg)} : proc{|msg| ws.send(msg)}
callback = @use_msgpack ? proc{|msg|
if (ws.tags.empty? || (ws.tags.include? msg[0]))
ws.send_binary(msg[1])
end
}
: proc{|msg|
if (ws.tags.empty? || (ws.tags.include? msg[0]))
ws.send(msg[1])
end
}

$lock.synchronize do
sid = $channel.subscribe callback
$log.trace "WebSocket connection: ID " + sid.to_s
Expand All @@ -47,8 +63,21 @@ def configure(conf)
}
end

#ws.onmessage { |msg|
#}
ws.onmessage { |msg|
if (msg == "reset")
ws.tags.clear()
elsif (msg.start_with? "add ")
tag = msg[4..-1]

if !ws.tags.include? tag
ws.tags << tag
end
elsif (msg.start_with? "del ")
ws.tags.delete(msg[4..-1])
end

$log.info "msg from client: #{msg} final tag #{ws.tags}"
}
}
end
}
Expand All @@ -74,7 +103,7 @@ def emit(tag, es, chain)
if (@add_tag) then data.unshift(tag) end
output = @use_msgpack ? data.to_msgpack : data.to_json
$lock.synchronize do
$channel.push output
$channel.push [tag, output]
end
}
end
Expand Down