参考 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")
}
});
返回错误:
推荐主题
主题 | 分类 | 回复 | 浏览 | 活动 |
---|---|---|---|---|
「每月明星 App」2014 年 11 月—— Anime Taste | 新闻公告 | 1 | 2.2K | 14-12-25 |
「产品更新」2015 年 2 月下旬到 3 月上旬 LeanCloud 更新总结 | 新闻公告 | 0 | 1.9K | 15-03-18 |
小程序如约而至,如何快速搭上这趟顺风车? | 新闻公告 | 2 | 2.5K | 17-01-9 |
「官方分享」使用 LeanCloud 与 React Native 构建原生应用 | 新闻公告 | 1 | 2.4K | 16-01-7 |
「产品更新」2014 年 12 月上旬更新汇总 | 新闻公告 | 0 | 1.5K | 14-12-12 |