参考 https://tyrus.java.net/documentation/1.13/index/2 这里,用java实现了一个websocket,在本地可以运行,但是部署到LeanEngine后不能正常访问,代码和相关访问方法如下:
public class MyAppConfig implements ServerApplicationConfig {
@Override
public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint>> set) {
return null;
}
@Override
public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> set) {
Set<Class<?>> s = new HashSet<>();
s.add(EventSocket.class);
return s;
}
}
@ClientEndpoint
@ServerEndpoint(value="/echo/")
public class EventSocket
{
Session session;
@OnOpen
public void onWebSocketConnect(Session sess)
{
System.out.println("Socket Connected: " + sess);
session = sess;
}
@OnMessage
public void onWebSocketText(String message) throws IOException {
System.out.println("Received TEXT message: " + message);
session.getBasicRemote().sendText(message);
}
@OnClose
public void onWebSocketClose(CloseReason reason)
{
System.out.println("Socket Closed: " + reason);
}
@OnError
public void onWebSocketError(Throwable cause)
{
cause.printStackTrace(System.err);
}
}
使用微信小程序提供的接口访问:
wx.connectSocket({
url: 'ws://leancloud.cn/echo/',
header: {
'Content-Type': 'application/json',
'X-LC-Id': 'xxx',
'X-LC-Key': 'xxx',
'X-LC-Prod': 1,
},
method:'POST',
success: function () {
console.log("ws_open success")
},
fail: function () {
console.log("ws_open fail")
}
});
返回错误:
推荐主题
主题 | 分类 | 回复 | 浏览 | 活动 |
---|---|---|---|---|
「故障说明」2014 年 12 月 12 日 15:00 的短暂故障说明 | 新闻公告 | 0 | 1.5K | 14-12-12 |
API、云引擎和文件域名绑定 Q&A | 新闻公告 | 4 | 5.7K | 21-10-15 |
「放假通知」LeanCloud 春节 2016 放假通知 | 新闻公告 | 0 | 1.8K | 16-02-2 |
未办理备案接入的域名将于 4 月限制访问 | 新闻公告 | 4 | 2.4K | 22-03-24 |
请通过域名绑定流程来配置自定义域名(9 月 2 日起不再支持直接解析至 LeanCloud 的域名) | 新闻公告 | 0 | 1.7K | 19-08-23 |