参考 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")
}
});
返回错误:
推荐主题
主题 | 分类 | 回复 | 浏览 | 活动 |
---|---|---|---|---|
「重大更新」云代码正式更名「云引擎」 ,3.0 版本重磅上线 | 新闻公告 | 14 | 5.7K | 15-06-25 |
「产品更新」LeanCloud 近期更新汇总 | 新闻公告 | 6 | 4.3K | 15-12-29 |
「产品更新」实时通信云代码集成发布 | 新闻公告 | 0 | 2.0K | 15-01-12 |
「合作活动」比 Uber 还酷 优步创客大赛 Hackathon 开发者脑洞大开 | 新闻公告 | 0 | 2.3K | 16-01-21 |
「产品笔记」回顾 2015 LeanCloud 十大产品革新 | 新闻公告 | 2 | 2.9K | 16-02-2 |