1
0
mirror of https://github.com/danbee/chess synced 2025-03-04 08:39:06 +00:00

Create a channel and connect the socket

This commit is contained in:
Daniel Barber 2018-02-25 15:07:45 -05:00
parent c0facfa4d5
commit 3b3f3e687a
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
5 changed files with 23 additions and 10 deletions

View File

@ -6,6 +6,8 @@ import { setGame, setGameId } from "../store/actions";
import ChessBoardSquare from "./chess-board-square"; import ChessBoardSquare from "./chess-board-square";
import socket from "../socket";
class ChessBoard extends React.Component { class ChessBoard extends React.Component {
componentWillMount() { componentWillMount() {
const { gameId, store } = this.props; const { gameId, store } = this.props;
@ -16,6 +18,15 @@ class ChessBoard extends React.Component {
.then((data) => { .then((data) => {
store.dispatch(setGame(data)); store.dispatch(setGame(data));
}); });
this.channel = socket.channel("game:" + gameId, {})
this.channel.join()
.receive("ok", resp => { console.log("Joined successfully", resp) })
.receive("error", resp => { console.log("Unable to join", resp) })
this.channel.on("game_update", data => {
store.dispatch(setGame(data));
})
} }
getBoard() { getBoard() {

View File

@ -53,10 +53,4 @@ let socket = new Socket("/socket", {params: {token: window.userToken}})
socket.connect() socket.connect()
// Now that you are connected, you can join channels with a topic:
let channel = socket.channel("topic:subtopic", {})
channel.join()
.receive("ok", resp => { console.log("Joined successfully", resp) })
.receive("error", resp => { console.log("Unable to join", resp) })
export default socket export default socket

View File

@ -0,0 +1,9 @@
defmodule ChessWeb.GameChannel do
@moduledoc false
use Phoenix.Channel
def join("game:" <> game_id, _params, socket) do
{:ok, socket}
end
end

View File

@ -2,7 +2,7 @@ defmodule ChessWeb.UserSocket do
use Phoenix.Socket use Phoenix.Socket
## Channels ## Channels
# channel "room:*", Chess.RoomChannel channel "game:*", ChessWeb.GameChannel
## Transports ## Transports
transport :websocket, Phoenix.Transports.WebSocket transport :websocket, Phoenix.Transports.WebSocket

View File

@ -25,7 +25,6 @@ defmodule ChessWeb.ChannelCase do
import Ecto.Changeset import Ecto.Changeset
import Ecto.Query import Ecto.Query
# The default endpoint for testing # The default endpoint for testing
@endpoint ChessWeb.Endpoint @endpoint ChessWeb.Endpoint
end end