Since I upgraded my project to Primefaces 5.0.4, Im facing some problems with Push. It just stops working, all I noticed was some expcetions that you can check below.
[Error] SyntaxError: JSON Parse error: Exponent symbols should be followed by an optional '+' or '-' and then by at least one number (jquery.js, line 25)
[Error] [Error] SyntaxError: JSON Parse error: Unable to parse JSON string [Error]
[Error] SyntaxError: JSON Parse error: Unexpected identifier "e4beb579"
parseJSON (jquery.js, line 25)
onMessage (push.js, line 1)
onMessage (push.js, line 1)
av (push.js, line 1)
ac (push.js, line 1)
D (push.js, line 1)
G (push.js, line 1)
(anonymous function) (push.js, line 1)
Note: This is really intermittent, there is not a way to always reproduce it.
My EndPoint:
Code: Select all
@PushEndpoint("/admin/sale/{channel}")
public class SalePushResource {
@OnMessage(encoders = { JSONEncoder.class })
public String onMessage(String message) {
return message;
}
}
Code: Select all
<p:socket onMessage="onSalesMessage" autoConnect="true"
channel="/admin/sale/#{salesBean.pushChannel}"
transport="websocket" fallbackTransport="jsonp" />
Code: Select all
public class SalePushBean implements Serializable {
private static final long serialVersionUID = 3217299041897220877L;
private Map<Integer, Channel> channels = new HashMap<>();
public void register(Long productId) {
Channel channel = getChannel(productId);
if (channel == null) {
channel = new Channel(UUID.randomUUID().toString());
channels.put(generateChannelHash(productId), channel);
} else {
channel.incNumOfListeners();
}
}
public void unregister(Long productId) {
Channel channel = getChannel(productId);
if (channel != null) {
if (channel.getNumOfListeners() == 1) {
channels.remove(generateChannelHash(productId));
} else {
channel.decNumOfListeners();
}
}
}
public String getPushChannel(Long productId) {
String channelName = null;
Channel channel = channels.get(generateChannelHash(productId));
if (channel != null) {
channelName = channel.getName();
} else {
register(productId);
channel = channels.get(generateChannelHash(productId));
channelName = channel.getName();
}
return channelName;
}
private Channel getChannel(Long productId) {
return channels.get(generateChannelHash(productId));
}
private void updateSaleFields(Long saleId) {
String channel = getPushChannel(saleId);
if (channel != null) {
EventBus eventBus = EventBusFactory.getDefault().eventBus();
eventBus.publish("/admin/sale/" + channel, "updateSales();");
}
}
public void updateSaleFields(Long productId, SaleEventType updateEventType) {
if (updateEventType.equals(SaleEventType.MODIFY)) {
updateSaleFields(productId);
} else {
updateSalesAllChannels();
}
}
private void updateSalesAllChannels() {
for (Channel channel : channels.values()) {
EventBus eventBus = EventBusFactory.getDefault().eventBus();
eventBus.publish("/admin/sale/" + channel.getName(), "updateSales();");
}
}
public int generateChannelHash(Long productId) {
final int prime = 31;
int result = 1;
result = prime * result + productId.hashCode();
return result;
}
}
There are 2 p:socket in the same page.
The Channel identifier is a hashcode.
Best Regards,