参考 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")
}
});
返回错误:
推荐主题
主题 | 分类 | 回复 | 浏览 | 活动 |
---|---|---|---|---|
小程序如约而至,如何快速搭上这趟顺风车? | 新闻公告 | 2 | 2.5K | 17-01-9 |
「重大更新」云代码正式更名「云引擎」 ,3.0 版本重磅上线 | 新闻公告 | 14 | 5.5K | 15-06-25 |
「人道主义」LeanCloud 春节放假通知 | 新闻公告 | 0 | 1.8K | 15-03-4 |
高效内存存储服务 LeanCache 正式发布 | 新闻公告 | 14 | 6.4K | 16-11-25 |
「重大更新」LeanCloud Python SDK 1.0.0 正式发布 | 新闻公告 | 2 | 2.3K | 15-03-20 |