Skip to content

Instantly share code, notes, and snippets.

@pjc0247
Created June 14, 2016 06:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjc0247/4260f7cd442469c8412d4301240eb331 to your computer and use it in GitHub Desktop.
Save pjc0247/4260f7cd442469c8412d4301240eb331 to your computer and use it in GitHub Desktop.

Service(Slack) As A MessageQ

슬랙을 메세지 큐로 사용하기 (공짜 서버 무임승차하기)

목적

슬랙을 게임 서버로 사용한다.
아래의 2_simple_server.rb는 간단하게 작성한 오브젝트 이동만 동기화되는 서버이다.
해당 수준의 서버정도는 슬랙과 함께라면 서버 코드 __0__줄로 처리할 수 있다.

메세징

채널에 보낸 메세지는 채널의 모든 사람에게 방송된다, 채널에 이동 패킷을 뿌리면 다른 클라이언트들이 받아서 해당 플레이어를 이동시킨다.

채널

채팅 시스템에서 채널은 기본적으로 구현되어 있다.
예를들어 월드맵에 메세지를 보내고 싶으면 #world 채널에 패킷을 보내면 되고, 특정 파티(그룹)에 메세지를 보내고 싶으면 #group_ID채널에 패킷을 보내면 된다.

슬랙봇

슬랙봇은 클라이언트끼리만으로 처리할 수 없는 (P2P 영역을 벗어난) 범위를 담당한다.
서버 공지사항 / 경매장 / 아이템 강화 / 이벤트 보상

require 'em-websocket'
require 'json'
$clients = []
$states = []
EM.run {
EM::WebSocket.run(:host => "0.0.0.0", :port => 9916) do |ws|
ws.onopen { |handshake|
puts "WebSocket connection open"
idx = 0
$clients.each do |client|
client.send(JSON.dump({
:c2dictionary => true,
:data => {
:type => "join",
:id => $clients.size,
:x => 100, :y => 100,
:angle => 0
}
}))
# 새로 들어온 클라이언트에게 기존 접속자 정보 보냄
ws.send(JSON.dump({
:c2dictionary => true,
:data => {
:type => "join",
:id => idx,
:x => $states[idx][:x], :y => $states[idx][:y],
:angle => $states[idx][:angle]
}
}))
idx += 1
end
$clients.push ws
$states.push({:x => 100, :y => 100, :angle => 0})
}
ws.onclose { puts "Connection closed" }
ws.onmessage { |msg|
puts msg
json = JSON.load msg
json = json["data"]
idx = $clients.find_index(ws)
if json["type"] == "move"
state = $states[idx]
state[:x] = json["x"].to_i
state[:y] = json["y"].to_i
state[:angle] = json["angle"].to_i
$clients.each do |client|
next if client == ws
client.send(JSON.dump({
:c2dictionary => true,
:data => {
:type => "move",
:id => idx,
:x => state[:x], :y => state[:y],
:angle => state[:angle]
}
}))
end
end
}
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment