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:
parent
c0facfa4d5
commit
3b3f3e687a
@ -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() {
|
||||||
|
|||||||
@ -3,9 +3,9 @@
|
|||||||
|
|
||||||
// To use Phoenix channels, the first step is to import Socket
|
// To use Phoenix channels, the first step is to import Socket
|
||||||
// and connect at the socket path in "lib/my_app/endpoint.ex":
|
// and connect at the socket path in "lib/my_app/endpoint.ex":
|
||||||
import {Socket} from "phoenix"
|
import { Socket } from "phoenix"
|
||||||
|
|
||||||
let socket = new Socket("/socket", {params: {token: window.userToken}})
|
let socket = new Socket("/socket", { params: { token: window.userToken } })
|
||||||
|
|
||||||
// When you connect, you'll often need to authenticate the client.
|
// When you connect, you'll often need to authenticate the client.
|
||||||
// For example, imagine you have an authentication plug, `MyAuth`,
|
// For example, imagine you have an authentication plug, `MyAuth`,
|
||||||
@ -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
|
||||||
|
|||||||
9
lib/chess_web/channels/game_channel.ex
Normal file
9
lib/chess_web/channels/game_channel.ex
Normal 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
|
||||||
@ -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
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user