ping event
This commit is contained in:
parent
ef4e0b4ac2
commit
521b3eef20
|
|
@ -7,13 +7,8 @@
|
||||||
<list default="true" id="8a64704d-5500-41a6-aa4c-e275933fc58c" name="Changes" comment="">
|
<list default="true" id="8a64704d-5500-41a6-aa4c-e275933fc58c" name="Changes" comment="">
|
||||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/internal/ws/handlers.go" beforeDir="false" afterPath="$PROJECT_DIR$/internal/ws/handlers.go" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/internal/ws/handlers.go" beforeDir="false" afterPath="$PROJECT_DIR$/internal/ws/handlers.go" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/internal/ws/hub.go" beforeDir="false" afterPath="$PROJECT_DIR$/internal/ws/hub.go" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/internal/ws/message.go" beforeDir="false" afterPath="$PROJECT_DIR$/internal/ws/message.go" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/internal/ws/message.go" beforeDir="false" afterPath="$PROJECT_DIR$/internal/ws/message.go" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/internal/ws/structs.go" beforeDir="false" afterPath="$PROJECT_DIR$/internal/ws/structs.go" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/../frontend/package.json" beforeDir="false" afterPath="$PROJECT_DIR$/../frontend/package.json" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/../frontend/src/interfaces/IMessage.ts" beforeDir="false" afterPath="$PROJECT_DIR$/../frontend/src/interfaces/IMessage.ts" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/../frontend/src/interfaces/IUser.ts" beforeDir="false" afterPath="$PROJECT_DIR$/../frontend/src/interfaces/IUser.ts" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/../frontend/types/environment.d.ts" beforeDir="false" afterPath="$PROJECT_DIR$/../frontend/types/environment.d.ts" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/../frontend/yarn.lock" beforeDir="false" afterPath="$PROJECT_DIR$/../frontend/yarn.lock" afterDir="false" />
|
|
||||||
</list>
|
</list>
|
||||||
<option name="SHOW_DIALOG" value="false" />
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ func handleIdentifyEvent(message *Message) {
|
||||||
Data: map[string]interface{}{
|
Data: map[string]interface{}{
|
||||||
"admin": true,
|
"admin": true,
|
||||||
"playlist": "",
|
"playlist": "",
|
||||||
"playHead": 0,
|
"playhead": 0,
|
||||||
"user": d["user"],
|
"user": d["user"],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
@ -33,3 +33,11 @@ func handlePingEvent(message *Message) {
|
||||||
}
|
}
|
||||||
message.send <- m.SerializeMessage().Data
|
message.send <- m.SerializeMessage().Data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleGetPlayhead(message *Message) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleSetPlayhead(message *Message) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,12 +24,18 @@ func NewHub() *Hub {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hub) handleMessage(rm RawMessage) {
|
func (h *Hub) handleMessage(rm RawMessage) {
|
||||||
m := rm.UnSerializeData()
|
m := rm.UnserializeData()
|
||||||
switch m.Type {
|
switch m.Type {
|
||||||
case Identify:
|
case Identify:
|
||||||
handleIdentifyEvent(&m)
|
handleIdentifyEvent(&m)
|
||||||
case Ping:
|
case Ping:
|
||||||
handlePingEvent(&m)
|
handlePingEvent(&m)
|
||||||
|
case Position:
|
||||||
|
handleGetPlayhead(&m)
|
||||||
|
case SetPosition:
|
||||||
|
handleSetPlayhead(&m)
|
||||||
|
default:
|
||||||
|
return
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ type RawMessage struct {
|
||||||
Data []byte
|
Data []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rm RawMessage) UnSerializeData() Message {
|
func (rm RawMessage) UnserializeData() Message {
|
||||||
var md MessageData
|
var md MessageData
|
||||||
if err := json.Unmarshal(rm.Data, &md); err != nil {
|
if err := json.Unmarshal(rm.Data, &md); err != nil {
|
||||||
log.Errorf("error unmarshalling message, %s", err.Error())
|
log.Errorf("error unmarshalling message, %s", err.Error())
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@
|
||||||
"@chakra-ui/theme-tools": "1.1.2",
|
"@chakra-ui/theme-tools": "1.1.2",
|
||||||
"@emotion/react": "11.1.5",
|
"@emotion/react": "11.1.5",
|
||||||
"@emotion/styled": "11.1.5",
|
"@emotion/styled": "11.1.5",
|
||||||
|
"consola": "^2.15.3",
|
||||||
"framer-motion": "^4.0.3",
|
"framer-motion": "^4.0.3",
|
||||||
"next": "latest",
|
"next": "latest",
|
||||||
"next-auth": "^4.1.2",
|
"next-auth": "^4.1.2",
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,8 @@ export enum MessageTypes {
|
||||||
Ping,
|
Ping,
|
||||||
Pong,
|
Pong,
|
||||||
Identify,
|
Identify,
|
||||||
Position,
|
GetPlayhead,
|
||||||
SetPosition,
|
SetPlayhead,
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IMessage {
|
interface IMessage {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import NextDocument, { Html, Head, Main, NextScript } from 'next/document'
|
import NextDocument, { Html, Head, Main, NextScript } from "next/document";
|
||||||
import { ColorModeScript } from '@chakra-ui/react'
|
import { ColorModeScript } from "@chakra-ui/react";
|
||||||
|
|
||||||
export default class Document extends NextDocument {
|
export default class Document extends NextDocument {
|
||||||
render() {
|
render() {
|
||||||
|
|
@ -13,6 +13,6 @@ export default class Document extends NextDocument {
|
||||||
<NextScript />
|
<NextScript />
|
||||||
</body>
|
</body>
|
||||||
</Html>
|
</Html>
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ export default NextAuth({
|
||||||
secret: process.env.SECRET,
|
secret: process.env.SECRET,
|
||||||
},
|
},
|
||||||
callbacks: {
|
callbacks: {
|
||||||
async session({ session, token, user }) {
|
async session({ session, token }) {
|
||||||
session.user.id = token.sub;
|
session.user.id = token.sub;
|
||||||
return session;
|
return session;
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import consola from "consola";
|
||||||
import { GetServerSideProps, NextPage } from "next";
|
import { GetServerSideProps, NextPage } from "next";
|
||||||
import { Session, User } from "next-auth";
|
import { Session, User } from "next-auth";
|
||||||
import { getSession, useSession } from "next-auth/react";
|
import { getSession, useSession } from "next-auth/react";
|
||||||
|
|
@ -14,7 +15,20 @@ interface PlayerPageProps {
|
||||||
URI: string;
|
URI: string;
|
||||||
user: User;
|
user: User;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const pingEvent = (ws: WebSocket) => {
|
||||||
|
let interval = setInterval(() => {
|
||||||
|
if (ws.readyState === ws.CLOSED) {
|
||||||
|
clearInterval(interval);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.log("running ping event");
|
||||||
|
ws.send(MessageUtil.encode(new Message(MessageTypes.Ping)));
|
||||||
|
}, 20000);
|
||||||
|
};
|
||||||
|
|
||||||
const PlayerPage: NextPage<PlayerPageProps> = ({ URI, user }) => {
|
const PlayerPage: NextPage<PlayerPageProps> = ({ URI, user }) => {
|
||||||
|
consola.wrapAll();
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (typeof window === "undefined") return;
|
if (typeof window === "undefined") return;
|
||||||
const ws = new WebSocket(URI);
|
const ws = new WebSocket(URI);
|
||||||
|
|
@ -30,6 +44,7 @@ const PlayerPage: NextPage<PlayerPageProps> = ({ URI, user }) => {
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
pingEvent(ws);
|
||||||
};
|
};
|
||||||
ws.onmessage = (event) => {
|
ws.onmessage = (event) => {
|
||||||
console.log(event);
|
console.log(event);
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ import IMessage, { MessageTypes } from "../interfaces/IMessage";
|
||||||
export default class Message implements IMessage {
|
export default class Message implements IMessage {
|
||||||
constructor(
|
constructor(
|
||||||
public type: MessageTypes,
|
public type: MessageTypes,
|
||||||
public data: Record<string, unknown>
|
public data?: Record<string, unknown>
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
toJSON(): Record<string, unknown> {
|
toJSON(): Record<string, unknown> {
|
||||||
|
|
|
||||||
|
|
@ -1344,6 +1344,11 @@ compute-scroll-into-view@1.0.14:
|
||||||
resolved "https://registry.yarnpkg.com/compute-scroll-into-view/-/compute-scroll-into-view-1.0.14.tgz#80e3ebb25d6aa89f42e533956cb4b16a04cfe759"
|
resolved "https://registry.yarnpkg.com/compute-scroll-into-view/-/compute-scroll-into-view-1.0.14.tgz#80e3ebb25d6aa89f42e533956cb4b16a04cfe759"
|
||||||
integrity sha512-mKDjINe3tc6hGelUMNDzuhorIUZ7kS7BwyY0r2wQd2HOH2tRuJykiC06iSEX8y1TuhNzvz4GcJnK16mM2J1NMQ==
|
integrity sha512-mKDjINe3tc6hGelUMNDzuhorIUZ7kS7BwyY0r2wQd2HOH2tRuJykiC06iSEX8y1TuhNzvz4GcJnK16mM2J1NMQ==
|
||||||
|
|
||||||
|
consola@^2.15.3:
|
||||||
|
version "2.15.3"
|
||||||
|
resolved "https://registry.yarnpkg.com/consola/-/consola-2.15.3.tgz#2e11f98d6a4be71ff72e0bdf07bd23e12cb61550"
|
||||||
|
integrity sha512-9vAdYbHj6x2fLKC4+oPH0kFzY/orMZyG2Aj+kNylHxKGJ/Ed4dpNyAQYwJOdqO4zdM7XpVHmyejQDcQHrnuXbw==
|
||||||
|
|
||||||
constants-browserify@1.0.0:
|
constants-browserify@1.0.0:
|
||||||
version "1.0.0"
|
version "1.0.0"
|
||||||
resolved "https://registry.yarnpkg.com/constants-browserify/-/constants-browserify-1.0.0.tgz#c20b96d8c617748aaf1c16021760cd27fcb8cb75"
|
resolved "https://registry.yarnpkg.com/constants-browserify/-/constants-browserify-1.0.0.tgz#c20b96d8c617748aaf1c16021760cd27fcb8cb75"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue