chenhg5/cc-connect · error

weibo: not connected

Error message

weibo: not connected

What it means

writeWS is the single serialized entry point for sending JSON frames over the Weibo WebSocket. It takes the mutex, then checks p.ws; if the connection was never established or has been closed/replaced, p.ws is nil and it returns 'weibo: not connected' instead of dereferencing nil. It surfaces a lifecycle problem: the platform is not currently online, so sendMessage, SendImage and SendFile all fail.

Source

Thrown at platform/weibo/weibo.go:631

					FileName: fname,
					Source:   &inputSource{Type: "base64", MediaType: mime, Data: b64},
				}},
			}},
		},
	}
	slog.Debug(p.tag()+": sending file", "to", rc.fromUserID, "name", fname, "size", len(file.Data))
	return p.writeWS(env)
}

func (p *Platform) writeWS(data any) error {
	// gorilla/websocket only allows one concurrent writer; wsMu must guard the
	// full WriteJSON call (pingLoop already follows this pattern), otherwise
	// concurrent sendMessage / SendImage / SendFile calls interleave frames
	// on the wire.
	p.wsMu.Lock()
	defer p.wsMu.Unlock()
	if p.ws == nil {
		return fmt.Errorf("weibo: not connected")
	}
	if err := p.ws.WriteJSON(data); err != nil {
		return fmt.Errorf("weibo: ws send: %w", err)
	}
	return nil
}

// --- Helpers ---

func (p *Platform) tag() string { return p.name }

func (p *Platform) isDuplicate(msgID string) bool {
	p.seenMu.Lock()
	defer p.seenMu.Unlock()
	if _, ok := p.seen[msgID]; ok {
		return true
	}
	if len(p.seen) >= maxSeenMessages {

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Retry after a short delay, giving the adapter's reconnect loop time to re-establish p.ws
  2. Check connection state / platform health before sending (e.g. via doctor or a readiness flag)
  3. Ensure Start() completes successfully before invoking any send API
  4. If persistently disconnected, verify network/proxy and Weibo endpoint reachability and restart the daemon

Example fix

if err := p.writeWS(env); err != nil {
    if strings.Contains(err.Error(), "not connected") {
        time.Sleep(reconnectBackoff)
        return p.writeWS(env) // retry after reconnect
    }
    return err
}
Defensive patterns

Strategy: retry

Validate before calling

if p == nil || !p.IsConnected() { // readiness check before sending
    return errors.New("weibo: platform not ready")
}

Try / catch

err := p.writeWS(env)
if errors.Is(err, errNotConnected) {
    select {
    case <-time.After(backoff):
    case <-ctx.Done():
        return ctx.Err()
    }
    return p.writeWS(env)
}

Prevention

When it happens

Trigger: Calling sendMessage/SendImage/SendFile before Platform.Start finished connecting; after the WebSocket dropped and reconnect is still in progress; after Stop was called; network outage where the ping loop has not yet re-established p.ws.

Common situations: Queued outgoing messages flushed during a network blip; a bot replying to an incoming message that arrived just as the socket closed; startup ordering where a cron/timer fires before the connection is up.

Understand the failure class

Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.

Related errors


AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06). Data as JSON: /api/errors/a53b99a51fe748b1. Report an issue: GitHub.