mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-03 01:42:50 +08:00
Add client poking (send and receive)
Uses tsclientlib's existing MessageTarget::Poke support end to end: a poke button on each client row opens a small compose overlay (an optional message, like TeamSpeak's own poke), and incoming pokes show as a dismissible, auto-expiring banner. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
76cb90360c
commit
ec034d2d6b
5 files changed
+214
-2
No files matched your search
+26
-2
@@ -12,7 +12,7 @@ use tokio::io::{AsyncBufReadExt, BufReader};
|
|||||||
|
|
||||||
use tsclientlib::audio::AudioHandler;
|
use tsclientlib::audio::AudioHandler;
|
||||||
use tsclientlib::events::Event as BookEvent;
|
use tsclientlib::events::Event as BookEvent;
|
||||||
use tsclientlib::messages::c2s::OutSendTextMessagePart;
|
use tsclientlib::messages::c2s::{OutClientPokeRequestPart, OutSendTextMessagePart};
|
||||||
use tsclientlib::prelude::*;
|
use tsclientlib::prelude::*;
|
||||||
use tsclientlib::{
|
use tsclientlib::{
|
||||||
data, ChannelId, ClientId, Connection, DisconnectOptions, MessageHandle, MessageTarget,
|
data, ChannelId, ClientId, Connection, DisconnectOptions, MessageHandle, MessageTarget,
|
||||||
@@ -77,6 +77,10 @@ enum Event {
|
|||||||
/// frontend can group messages into one thread per partner.
|
/// frontend can group messages into one thread per partner.
|
||||||
#[serde(rename = "privateMessage")]
|
#[serde(rename = "privateMessage")]
|
||||||
PrivateMessage { partner_id: u16, partner_name: String, from_self: bool, message: String },
|
PrivateMessage { partner_id: u16, partner_name: String, from_self: bool, message: String },
|
||||||
|
/// Someone poked us. Pokes can only be received here - the server doesn't
|
||||||
|
/// echo our own outgoing pokes back to us, unlike text messages.
|
||||||
|
#[serde(rename = "poke")]
|
||||||
|
Poke { from: String, message: String },
|
||||||
/// Mixed, decoded PCM audio ready to play: 16-bit signed little-endian,
|
/// Mixed, decoded PCM audio ready to play: 16-bit signed little-endian,
|
||||||
/// stereo, 48kHz, base64-encoded.
|
/// stereo, 48kHz, base64-encoded.
|
||||||
#[serde(rename = "audioOut")]
|
#[serde(rename = "audioOut")]
|
||||||
@@ -346,6 +350,24 @@ async fn run(args: Args) -> Result<()> {
|
|||||||
},
|
},
|
||||||
None => emit(&Event::Error { message: "Malformed pm command".into() }),
|
None => emit(&Event::Error { message: "Malformed pm command".into() }),
|
||||||
}
|
}
|
||||||
|
} else if let Some(rest) = l.strip_prefix("poke ") {
|
||||||
|
// Unlike chat, a poke's message is optional - a plain poke with no
|
||||||
|
// text is a normal thing to send in TeamSpeak.
|
||||||
|
let rest = rest.trim();
|
||||||
|
let (id, message) = rest.split_once(' ').unwrap_or((rest, ""));
|
||||||
|
match id.parse::<u16>() {
|
||||||
|
Ok(id) => {
|
||||||
|
let part =
|
||||||
|
OutClientPokeRequestPart { client_id: ClientId(id), message: Cow::Borrowed(message) };
|
||||||
|
match part.send_with_result(&mut con) {
|
||||||
|
Ok(handle) => {
|
||||||
|
pending_messages.insert(handle, "Poke".into());
|
||||||
|
}
|
||||||
|
Err(e) => emit(&Event::Error { message: e.to_string() }),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => emit(&Event::Error { message: format!("Invalid client id: {id}") }),
|
||||||
|
}
|
||||||
} else if let Some(b64) = l.strip_prefix("audio ") {
|
} else if let Some(b64) = l.strip_prefix("audio ") {
|
||||||
match base64::engine::general_purpose::STANDARD.decode(b64.trim()) {
|
match base64::engine::general_purpose::STANDARD.decode(b64.trim()) {
|
||||||
Ok(bytes) => {
|
Ok(bytes) => {
|
||||||
@@ -425,7 +447,9 @@ async fn run(args: Args) -> Result<()> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
MessageTarget::Poke(_) => {
|
||||||
|
emit(&Event::Poke { from: invoker.name.clone(), message: message.clone() });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,6 +42,10 @@ wss.on("connection", (socket: WebSocket) => {
|
|||||||
await connection?.sendPrivateMessage(msg.clientId, msg.message);
|
await connection?.sendPrivateMessage(msg.clientId, msg.message);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case "sendPoke": {
|
||||||
|
await connection?.sendPoke(msg.clientId, msg.message ?? "");
|
||||||
|
break;
|
||||||
|
}
|
||||||
case "sendAudio": {
|
case "sendAudio": {
|
||||||
await connection?.sendAudio(msg.pcm);
|
await connection?.sendAudio(msg.pcm);
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ export type Ts3ConnectionEvent =
|
|||||||
| { type: "chatMessage"; from: string; message: string }
|
| { type: "chatMessage"; from: string; message: string }
|
||||||
| { type: "serverMessage"; from: string; message: string }
|
| { type: "serverMessage"; from: string; message: string }
|
||||||
| { type: "privateMessage"; partnerId: number; partnerName: string; fromSelf: boolean; message: string }
|
| { type: "privateMessage"; partnerId: number; partnerName: string; fromSelf: boolean; message: string }
|
||||||
|
| { type: "poke"; from: string; message: string }
|
||||||
| { type: "audioOut"; pcm: string }
|
| { type: "audioOut"; pcm: string }
|
||||||
| { type: "talkers"; clients: number[] }
|
| { type: "talkers"; clients: number[] }
|
||||||
| { type: "disconnected"; reason: string }
|
| { type: "disconnected"; reason: string }
|
||||||
@@ -92,6 +93,7 @@ export class Ts3Connection {
|
|||||||
| { type: "chatMessage"; from: string; message: string }
|
| { type: "chatMessage"; from: string; message: string }
|
||||||
| { type: "serverMessage"; from: string; message: string }
|
| { type: "serverMessage"; from: string; message: string }
|
||||||
| { type: "privateMessage"; partner_id: number; partner_name: string; from_self: boolean; message: string }
|
| { type: "privateMessage"; partner_id: number; partner_name: string; from_self: boolean; message: string }
|
||||||
|
| { type: "poke"; from: string; message: string }
|
||||||
| { type: "audioOut"; pcm: string }
|
| { type: "audioOut"; pcm: string }
|
||||||
| { type: "talkers"; clients: number[] }
|
| { type: "talkers"; clients: number[] }
|
||||||
| { type: "disconnected"; reason: string }
|
| { type: "disconnected"; reason: string }
|
||||||
@@ -162,6 +164,11 @@ export class Ts3Connection {
|
|||||||
if (sanitized) this.child?.stdin.write(`pm ${clientId} ${sanitized}\n`);
|
if (sanitized) this.child?.stdin.write(`pm ${clientId} ${sanitized}\n`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async sendPoke(clientId: number, message: string): Promise<void> {
|
||||||
|
const sanitized = message.replace(/[\r\n]+/g, " ").trim();
|
||||||
|
this.child?.stdin.write(`poke ${clientId} ${sanitized}\n`);
|
||||||
|
}
|
||||||
|
|
||||||
async sendAudio(pcmBase64: string): Promise<void> {
|
async sendAudio(pcmBase64: string): Promise<void> {
|
||||||
this.child?.stdin.write(`audio ${pcmBase64}\n`);
|
this.child?.stdin.write(`audio ${pcmBase64}\n`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -218,6 +218,25 @@
|
|||||||
opacity: 0.85;
|
opacity: 0.85;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.ts-poke-button {
|
||||||
|
margin-left: auto;
|
||||||
|
background: transparent;
|
||||||
|
border: none;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 11px;
|
||||||
|
padding: 0 0.2rem;
|
||||||
|
opacity: 0;
|
||||||
|
transition: opacity 0.1s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.ts-client-row:hover .ts-poke-button {
|
||||||
|
opacity: 0.85;
|
||||||
|
}
|
||||||
|
|
||||||
|
.ts-poke-button:hover {
|
||||||
|
opacity: 1 !important;
|
||||||
|
}
|
||||||
|
|
||||||
.ts-mic-on {
|
.ts-mic-on {
|
||||||
background: color-mix(in srgb, #4caf50 30%, var(--button-bg-1)) !important;
|
background: color-mix(in srgb, #4caf50 30%, var(--button-bg-1)) !important;
|
||||||
}
|
}
|
||||||
@@ -363,3 +382,81 @@
|
|||||||
.ts-connect-error button:hover {
|
.ts-connect-error button:hover {
|
||||||
opacity: 1;
|
opacity: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.ts-poke-compose-backdrop {
|
||||||
|
position: fixed;
|
||||||
|
inset: 0;
|
||||||
|
background: rgba(0, 0, 0, 0.35);
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
z-index: 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
.ts-poke-compose {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.5rem;
|
||||||
|
padding: 0.9rem 1.1rem;
|
||||||
|
background: var(--bg-toolbar-1);
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: 6px;
|
||||||
|
color: var(--text);
|
||||||
|
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
|
||||||
|
}
|
||||||
|
|
||||||
|
.ts-poke-compose input {
|
||||||
|
background: var(--input-bg);
|
||||||
|
color: var(--input-text);
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: 3px;
|
||||||
|
padding: 0.3rem 0.5rem;
|
||||||
|
min-width: 220px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.ts-poke-compose button {
|
||||||
|
background: linear-gradient(var(--button-bg-1), var(--button-bg-2));
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: 3px;
|
||||||
|
color: var(--text);
|
||||||
|
cursor: pointer;
|
||||||
|
padding: 0.3rem 0.7rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.ts-poke-compose button:hover {
|
||||||
|
background: linear-gradient(var(--button-bg-hover-1), var(--button-bg-hover-2));
|
||||||
|
}
|
||||||
|
|
||||||
|
.ts-poke-notice {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 0.75rem;
|
||||||
|
padding: 0.5rem 0.9rem;
|
||||||
|
background: #e8a53d;
|
||||||
|
color: #1a1a1a;
|
||||||
|
font-size: 13px;
|
||||||
|
animation: ts-poke-flash 0.4s ease-in-out 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes ts-poke-flash {
|
||||||
|
0%, 100% {
|
||||||
|
filter: brightness(1);
|
||||||
|
}
|
||||||
|
50% {
|
||||||
|
filter: brightness(1.3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.ts-poke-notice button {
|
||||||
|
background: transparent;
|
||||||
|
border: none;
|
||||||
|
color: #1a1a1a;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 13px;
|
||||||
|
opacity: 0.7;
|
||||||
|
}
|
||||||
|
|
||||||
|
.ts-poke-notice button:hover {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
@@ -87,6 +87,7 @@ function ChannelTree({
|
|||||||
talkers,
|
talkers,
|
||||||
onSelectChannel,
|
onSelectChannel,
|
||||||
onOpenPrivateChat,
|
onOpenPrivateChat,
|
||||||
|
onPokeClient,
|
||||||
}: {
|
}: {
|
||||||
channels: ChannelInfo[];
|
channels: ChannelInfo[];
|
||||||
clients: ClientInfo[];
|
clients: ClientInfo[];
|
||||||
@@ -95,6 +96,7 @@ function ChannelTree({
|
|||||||
talkers: Set<number>;
|
talkers: Set<number>;
|
||||||
onSelectChannel: (channelId: number) => void;
|
onSelectChannel: (channelId: number) => void;
|
||||||
onOpenPrivateChat: (clientId: number, clientName: string) => void;
|
onOpenPrivateChat: (clientId: number, clientName: string) => void;
|
||||||
|
onPokeClient: (clientId: number, clientName: string) => void;
|
||||||
}) {
|
}) {
|
||||||
const children = channels.filter((c) => c.parent === parent).sort((a, b) => a.order - b.order);
|
const children = channels.filter((c) => c.parent === parent).sort((a, b) => a.order - b.order);
|
||||||
if (children.length === 0) return null;
|
if (children.length === 0) return null;
|
||||||
@@ -122,6 +124,18 @@ function ChannelTree({
|
|||||||
<ClientIcon />
|
<ClientIcon />
|
||||||
<span>{c.name}</span>
|
<span>{c.name}</span>
|
||||||
<ClientStatusIcons client={c} />
|
<ClientStatusIcons client={c} />
|
||||||
|
{c.id !== ownClientId && (
|
||||||
|
<button
|
||||||
|
className="ts-poke-button"
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
onPokeClient(c.id, c.name);
|
||||||
|
}}
|
||||||
|
title={`Poke ${c.name}`}
|
||||||
|
>
|
||||||
|
👉
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
))}
|
))}
|
||||||
@@ -134,6 +148,7 @@ function ChannelTree({
|
|||||||
talkers={talkers}
|
talkers={talkers}
|
||||||
onSelectChannel={onSelectChannel}
|
onSelectChannel={onSelectChannel}
|
||||||
onOpenPrivateChat={onOpenPrivateChat}
|
onOpenPrivateChat={onOpenPrivateChat}
|
||||||
|
onPokeClient={onPokeClient}
|
||||||
/>
|
/>
|
||||||
</li>
|
</li>
|
||||||
))}
|
))}
|
||||||
@@ -160,6 +175,12 @@ interface PmThread {
|
|||||||
|
|
||||||
type ActiveTab = "channel" | "server" | number;
|
type ActiveTab = "channel" | "server" | number;
|
||||||
|
|
||||||
|
interface PokeNotice {
|
||||||
|
id: number;
|
||||||
|
from: string;
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [host, setHost] = useState(() => localStorage.getItem(LAST_HOST_KEY) ?? "localhost");
|
const [host, setHost] = useState(() => localStorage.getItem(LAST_HOST_KEY) ?? "localhost");
|
||||||
const [nickname, setNickname] = useState(() => localStorage.getItem(LAST_NICKNAME_KEY) ?? "Claude Code");
|
const [nickname, setNickname] = useState(() => localStorage.getItem(LAST_NICKNAME_KEY) ?? "Claude Code");
|
||||||
@@ -176,6 +197,9 @@ function App() {
|
|||||||
const [serverChat, setServerChat] = useState<ChatEntry[]>([]);
|
const [serverChat, setServerChat] = useState<ChatEntry[]>([]);
|
||||||
const [chatInput, setChatInput] = useState("");
|
const [chatInput, setChatInput] = useState("");
|
||||||
const [pmThreads, setPmThreads] = useState<Record<number, PmThread>>({});
|
const [pmThreads, setPmThreads] = useState<Record<number, PmThread>>({});
|
||||||
|
const [pokes, setPokes] = useState<PokeNotice[]>([]);
|
||||||
|
const [pokeTarget, setPokeTarget] = useState<{ id: number; name: string } | null>(null);
|
||||||
|
const [pokeMessage, setPokeMessage] = useState("");
|
||||||
const [activeTab, setActiveTab] = useState<ActiveTab>("channel");
|
const [activeTab, setActiveTab] = useState<ActiveTab>("channel");
|
||||||
const [theme, setTheme] = useState<"light" | "dark">(() =>
|
const [theme, setTheme] = useState<"light" | "dark">(() =>
|
||||||
window.matchMedia?.("(prefers-color-scheme: dark)").matches ? "dark" : "light"
|
window.matchMedia?.("(prefers-color-scheme: dark)").matches ? "dark" : "light"
|
||||||
@@ -194,6 +218,7 @@ function App() {
|
|||||||
const micCaptureRef = useRef<MicCapture | null>(null);
|
const micCaptureRef = useRef<MicCapture | null>(null);
|
||||||
const activeTabRef = useRef<ActiveTab>("channel");
|
const activeTabRef = useRef<ActiveTab>("channel");
|
||||||
const hasConnectedRef = useRef(false);
|
const hasConnectedRef = useRef(false);
|
||||||
|
const pokeIdRef = useRef(0);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
chatEndRef.current?.scrollIntoView({ block: "nearest" });
|
chatEndRef.current?.scrollIntoView({ block: "nearest" });
|
||||||
@@ -313,6 +338,12 @@ function App() {
|
|||||||
case "talkers":
|
case "talkers":
|
||||||
setTalkers(new Set<number>(data.clients));
|
setTalkers(new Set<number>(data.clients));
|
||||||
break;
|
break;
|
||||||
|
case "poke": {
|
||||||
|
const id = ++pokeIdRef.current;
|
||||||
|
setPokes((prev) => [...prev, { id, from: data.from, message: data.message }]);
|
||||||
|
setTimeout(() => setPokes((prev) => prev.filter((p) => p.id !== id)), 10000);
|
||||||
|
break;
|
||||||
|
}
|
||||||
case "disconnected":
|
case "disconnected":
|
||||||
hasConnectedRef.current = false;
|
hasConnectedRef.current = false;
|
||||||
setConnecting(false);
|
setConnecting(false);
|
||||||
@@ -322,6 +353,7 @@ function App() {
|
|||||||
setChat([]);
|
setChat([]);
|
||||||
setServerChat([]);
|
setServerChat([]);
|
||||||
setPmThreads({});
|
setPmThreads({});
|
||||||
|
setPokes([]);
|
||||||
setActiveTab("channel");
|
setActiveTab("channel");
|
||||||
setTalkers(new Set());
|
setTalkers(new Set());
|
||||||
stopMic();
|
stopMic();
|
||||||
@@ -425,6 +457,20 @@ function App() {
|
|||||||
setActiveTab(clientId);
|
setActiveTab(clientId);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handlePokeClient = (clientId: number, clientName: string) => {
|
||||||
|
setPokeTarget({ id: clientId, name: clientName });
|
||||||
|
setPokeMessage("");
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSendPoke = () => {
|
||||||
|
if (!pokeTarget) return;
|
||||||
|
socketRef.current?.send(
|
||||||
|
JSON.stringify({ type: "sendPoke", clientId: pokeTarget.id, message: pokeMessage })
|
||||||
|
);
|
||||||
|
setPokeTarget(null);
|
||||||
|
setPokeMessage("");
|
||||||
|
};
|
||||||
|
|
||||||
const handleClosePrivateChat = (clientId: number) => {
|
const handleClosePrivateChat = (clientId: number) => {
|
||||||
setPmThreads((prev) => {
|
setPmThreads((prev) => {
|
||||||
const next = { ...prev };
|
const next = { ...prev };
|
||||||
@@ -553,6 +599,39 @@ function App() {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{pokeTarget && (
|
||||||
|
<div className="ts-poke-compose-backdrop" onClick={() => setPokeTarget(null)}>
|
||||||
|
<div className="ts-poke-compose" onClick={(e) => e.stopPropagation()}>
|
||||||
|
<span>
|
||||||
|
👉 Poke <strong>{pokeTarget.name}</strong>
|
||||||
|
</span>
|
||||||
|
<input
|
||||||
|
autoFocus
|
||||||
|
value={pokeMessage}
|
||||||
|
onChange={(e) => setPokeMessage(e.target.value)}
|
||||||
|
onKeyDown={(e) => {
|
||||||
|
if (e.key === "Enter") handleSendPoke();
|
||||||
|
if (e.key === "Escape") setPokeTarget(null);
|
||||||
|
}}
|
||||||
|
placeholder="Optional message..."
|
||||||
|
/>
|
||||||
|
<button onClick={handleSendPoke}>Poke</button>
|
||||||
|
<button onClick={() => setPokeTarget(null)}>Cancel</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{pokes.map((poke) => (
|
||||||
|
<div key={poke.id} className="ts-poke-notice">
|
||||||
|
<span>
|
||||||
|
👉 <strong>{poke.from}</strong> poked you{poke.message ? `: ${poke.message}` : ""}
|
||||||
|
</span>
|
||||||
|
<button onClick={() => setPokes((prev) => prev.filter((p) => p.id !== poke.id))} title="Dismiss">
|
||||||
|
✕
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
|
||||||
<div className="ts-body">
|
<div className="ts-body">
|
||||||
<div className="ts-tree-panel">
|
<div className="ts-tree-panel">
|
||||||
{connected ? (
|
{connected ? (
|
||||||
@@ -569,6 +648,7 @@ function App() {
|
|||||||
talkers={displayTalkers}
|
talkers={displayTalkers}
|
||||||
onSelectChannel={handleSelectChannel}
|
onSelectChannel={handleSelectChannel}
|
||||||
onOpenPrivateChat={handleOpenPrivateChat}
|
onOpenPrivateChat={handleOpenPrivateChat}
|
||||||
|
onPokeClient={handlePokeClient}
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
) : (
|
) : (
|
||||||
|
|||||||
Reference in new issue
Block a user